【问题标题】:Is it possible to get the image path of an image in an image view?是否可以在图像视图中获取图像的图像路径?
【发布时间】:2012-06-08 15:22:53
【问题描述】:

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

更新:

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

【问题讨论】:

  • 既然已经选择了ImagePath,为什么还需要一个图像路径?您的意思是获取缩放位图的路径吗?恕我直言,缩放位图存储在内存中,您无法获得它的路径(除非您坚持它).. p.s.如果要保留位图stackoverflow.com/questions/649154/…,请选中此项
  • 我怎样才能得到那个缩放图像的路径??
  • 你不能,你可以得到它的引用。另一方面,如果确实需要,您可以使用该引用并将图像刷新到磁盘以供以后使用(尽管我仍然不明白为什么您需要使用路径而不是图像引用??)。

标签: android sql image path


【解决方案1】:

不是直接的,一旦图像被设置在ImageView 上,它就会变成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 的子类来封装这个额外的函数,以帮助您的代码更易于阅读和维护。

HTH

【讨论】:

  • 我的代码看起来如何?什么时候设置标签?查看更新。
  • 在最后一行之后添加mImageView.setTag(selectedImagePath) 行(即setImageBitmap() 之后)。然后,根据路径的对象类型(String、Uri 等),您可能需要修改检索示例中的转换。
猜你喜欢
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
相关资源
最近更新 更多