1

我在图像视图中设置了图像。现在我想保存这个状态并且需要知道那个图像的图像路径。有没有办法做到这一点?

更新:

// 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);
4

1 回答 1

3

不是直接的,一旦图像被设置在 an 上,ImageView它就会变成 a Drawable,而其他所有内容都被遗忘了。但是,您可以为此目的使用标签。任何视图都可以有一个与之关联的标签,该标签表示有关该视图的一些元数据。您可以执行以下操作:

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();

您还可以考虑创建一个子类ImageView来封装这个额外的函数,以帮助您的代码更易于阅读和维护。

高温高压

于 2012-06-04T14:48:45.887 回答