Retrive从乌里绘制资源 [英] Retrive drawable resource from Uri
本文介绍了Retrive从乌里绘制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我存储(因为某些原因)在乌里的绘制资源路径是这样的:
I'm storing (for some reasons) the drawable resource path in an Uri in this way:
Uri uri = Uri.parse("android.resource://my_app_package/drawable/drawable_name");
现在我想获得该URI是引用绘制对象。 我怎样才能做到这一点,好吗?
Now I would like to get the Drawable that the uri is referencing. How can I do that, please?
我不希望把它放在一个ImageView的,只是retrive的绘制对象(或位图)对象。
I don't want to put it in an ImageView, just retrive the Drawable (or Bitmap) object.
推荐答案
getContentResolver CR对象,然后调用:
getContentResolver cr object then call:
是= cr.openInputStream(URI)
is = cr.openInputStream(uri)
和最后呼叫:
Drawable.createFromStream(InputStream的是,字符串源名)
Drawable.createFromStream(InputStream is, String srcName)
...这里有一个实际的工作code例如:
...Here's an actual working code example:
try {
InputStream inputStream = getContentResolver().openInputStream(yourUri);
yourDrawable = Drawable.createFromStream(inputStream, yourUri.toString() );
} catch (FileNotFoundException e) {
yourDrawable = getResources().getDrawable(R.drawable.default_image);
}
这篇关于Retrive从乌里绘制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文