1

I have set an image in an image view. Now I want to save this state and need to know the image path of that image. Is there a way to do that?

UPDATE:

// Decode, scale and set the image.
            Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
            myBitmap.recycle();
            myBitmap = null;

            mImageView.setImageBitmap(scaledBitmap);
user1420042
  • 1,689
  • 9
  • 35
  • 53
  • Why would you need an image path when you already have selectedImagePath? Do you mean to get the path for the scaled bitmap instead? IMHO the scaled bitmap is stored in memory and you can't get the path to it (unless you persist it).. p.s. check this if you want to persist the bitmap http://stackoverflow.com/questions/649154/android-bitmap-save-to-location – sinek Jun 04 '12 at 14:59
  • and how can I get the path of that scaled image?? – user1420042 Jun 04 '12 at 15:09
  • You can't, you can just get the reference to it. On the other side, you can use that reference and flush image to disk for later usage if really needed (though I still don't understand why you need to use path instead of image reference??). – sinek Jun 04 '12 at 15:18

1 Answers1

3

Not directly, once an image is set on an ImageView it is turned into a Drawable and all else is forgotten. However, you could use tags for this purpose. Any view can have a tag associated with it that represents some metadata about that view. You could do something like:

ImageView iv; //Declared elsewhere
Uri imagePath = Uri.parse("...");
//Set the image itself
iv.setImageURI(imagePath);
//Add the path value as a tag
iv.setTag(imagePath);


//Sometime in the future, retrieve it
Uri path = (Uri)iv.getTag();

You might also consider creating a subclass of ImageView to encapsulate this extra function to help make your code a little easier to read and maintain.

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • How would that look like for my code? When do set the tag? See the Update. – user1420042 Jun 04 '12 at 14:54
  • Add the line `mImageView.setTag(selectedImagePath)` after your last line (i.e. after `setImageBitmap()`). Then, depending on what object type the path is (String, Uri, etc.) you may need to modify the cast in the retrieval example. – devunwired Jun 04 '12 at 15:30