Get the name of the file in which your ImageView uploaded its image from

I am looking for a built-in method of the ImageView object to tell me which file I received my image from.

For example, earlier in the code that I called ...

ImageView iv = new ImageView(MainActivity.this);                
iv.setImageBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()));

Suppose I no longer have a reference to 'f' and you need to find out what f is. Is there any way to do this? My job is to create a class that extends ImageView and supports String. (Or create a map between ImageView and String)

Recommendations

+2
source share
3 answers

The simplest workaround would probably be to use a tag for ImageView:

ImageView iv = new ImageView(MainActivity.this);
String path = f.getAbsolutePath();                 
iv.setImageBitmap(BitmapFactory.decodeFile(path));
iv.setTag(path);

Getting the path:

String path = (String) iv.getTag();
+8
source

, BitmapFactory.decodeFile - . Bitmap ( , , ), , .

- , /. , , , , Pair , , nitpicking.

+1
// GET THE URI FROM THE IMAGE (YOU NEED A BITMAP FOR THIS -  WORKAROUND FOLLOWS)
Uri tempUri = getImageUri(getApplicationContext(), bitmap);

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

// THE WORKAROUND (OPTIONAL IN CASE YOU DO NOT HAVE A BITMAP)
Bitmap bitmap = ((BitmapDrawable)youImageView.getDrawable()).getBitmap();

: fooobar.com/questions/26543/...

// NOW THAT YOU WILL HAVE THE URI, USE THIS TO GET THE ABSOLUTE PATH OF THE IMAGE
File finalFile = new File(getRealPathFromURI(tempUri));

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

In my experience with Android, the only need I have ever had was that I started working on the Twitter4J SDK and TwitPic API. TwitPic needs the absolute path of the image you want to download. It was the best solution I found (a combination of several different solutions actually), but it always worked like a charm. Hope this helps you.

0
source

Source: https://habr.com/ru/post/1599853/


All Articles