gpt4 book ai didi

android - 如何从 Drawable 获取 URI

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:35 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我在其中提供一些内置图像,并让用户可以选择从 Web 下载更多图像以在应用程序中使用。在我的应用程序的某个时刻,我查看布局中的 ImageView 并想确定里面的 Drawable 是内置资源还是我从 Web 下载到 SD 卡的图像。

有没有办法提取ImageView中使用的Drawable的URI?这样我就可以查看它是资源还是下载的文件。

到目前为止,这是我的代码:

ImageView view = (ImageView) layout.findViewById(R.id.content_img);
Drawable image = view.getDrawable();

更新:使用下面 Barry Fruitman 的建议,我将图像的 URI 直接存储在我的自定义 ImageView 中以备后用。这是我的实现的样子:

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
}

最佳答案

没有。一旦您创建了 Drawable,该信息就会丢失。我建议您做的是子类化 ImageView 并添加额外的成员来跟踪您想要的任何内容。

替换:

<ImageView />

<com.mypackage.MyImageView />

并创建:

class MyImageView extends ImageView {
protected final int LOCAL_IMAGE = 1;
protected final int REMOTE_IMAGE = 2;
protected int imageType;
}

MyImageView 的行为与 ImageView 完全一样,但是有了这个额外的成员,您可以在任何地方读写。您可能还必须使用仅调用 super() 的构造函数覆盖 ImageView 构造函数。

关于android - 如何从 Drawable 获取 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com