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

查看:83
本文介绍了从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天全站免登陆