我正在开发一款Android应用程序,其中我提供了一些内置图像,并让用户选择从网络下载更多图像来使用。在我的应用程序的某个点上,我查看布局中的ImageView并希望确定其中的Drawable是内置资源还是我从Web下载到SD卡中的图像。
有没有办法提取ImageView中使用的Drawable的URI?这样我就可以看到它是资源还是已下载文件。
以下是我的代码:
有没有办法提取ImageView中使用的Drawable的URI?这样我就可以看到它是资源还是已下载文件。
以下是我的代码:
ImageView view = (ImageView) layout.findViewById(R.id.content_img);
Drawable image = view.getDrawable();
更新: 根据Barry Fruitman的建议,我直接在自定义的ImageView中存储了图像的URI以供后续使用。以下是我的实现:
public class MemoryImageView extends ImageView {
private String storedUri = null;
public MemoryImageView(Context context, String Uri) {
super(context);
}
public MemoryImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MemoryImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public String getStoredUri() {
return storedUri;
}
public void setStoredUri(String storedUri) {
this.storedUri = storedUri;
}
使用方法如下:
}
MemoryImageView view = (MemoryImageView) layout.findViewById(R.id.content_img);
String img = view.getStoredUri();
if(img.startsWith("android.resource")) {
//in-built resource
} else {
//downloaded image
}