2

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

For example earlier in the code I called...

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

Assume I no longer have a reference to 'f' and need to find out what f was. Is there a way to do that? My work around is creating a class that extends ImageView and has a String pared up with it. (Or create a Map between an ImageView and a String)

Recommendations?

Matthew
  • 3,886
  • 7
  • 47
  • 84

3 Answers3

9

The easiest workaround would be probably using a tag for ImageView:

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

Retrieving the path:

String path = (String) iv.getTag();
cyanide
  • 3,885
  • 3
  • 25
  • 33
  • 16
    What is f.getAbsolutePath()? – DevC Jan 13 '14 at 11:57
  • @DevC the f is probably a File object. If you getting your image from the image picker, more common case, a URI object will be retrieved by Intent.getData() and will be set with ImageView.setImageURI() instead. – Allan Veloso Aug 26 '19 at 19:04
1

As far as I know all BitmapFactory.decodeFile returns is a Bitmap. Since the Bitmap could come from any source (not all having a path, e.g. from RAM), it does not have the file path encapsulated in it.

About your workaround - any will do, but choosing one depends on your memory/speed requirements. For example, if all you have is one pair, you could use Pair instead of the map, or you could just keep the path next to the bitmap - but that's just nitpicking.

scf
  • 396
  • 2
  • 19
0
// 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();

Credit: https://stackoverflow.com/a/8306683/450534

// 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 ever had this was when I started working on the Twitter4J SDK and the TwitPic API. TwitPic needs the absolute path of the Image you want to upload. This was the best solution I found (a combination of several different solutions actually) but it has always worked like a charm. Hope it helps you.

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151