从 Uri 检索可绘制资源 [英] Retrieve drawable resource from Uri

查看:37
本文介绍了从 Uri 检索可绘制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式将可绘制资源路径存储在 Uri 中:

I'm storing the drawable resource path in an Uri in this way:

Uri uri = Uri.parse("android.resource://my_app_package/drawable/drawable_name");

如何获取 uri 引用的 Drawable?

How do I get the Drawable that the uri is referencing?

我不想把它放在 ImageView 中,只要检索 Drawable(或 Bitmap)对象即可.

I don't want to put it in an ImageView, just retrieve the Drawable (or Bitmap) object.

推荐答案

getContentResolver cr object 然后调用:

is = cr.openInputStream(uri) 

最后调用:

Drawable.createFromStream(InputStream is, String srcName)

...这是一个实际的工作代码示例,您可以自己查看:

...Here's an actual working code example so you can see for yourself:

try {
    InputStream inputStream = getContentResolver().openInputStream(yourUri);
    yourDrawable = Drawable.createFromStream(inputStream, yourUri.toString() );
} catch (FileNotFoundException e) {
    yourDrawable = getResources().getDrawable(R.drawable.default_image);
}

这篇关于从 Uri 检索可绘制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆