获取存储在 drawable 中的图像的 URI [英] Get the URI of an image stored in drawable

查看:20
本文介绍了获取存储在 drawable 中的图像的 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中添加了几个示例项目,因此当用户第一次看到它时,它看起来不会那么空.包含示例项目的列表应该有一个图像,我要使用的图像已经存储在应用程序的/res/drawable-folder 中.

I am adding a couple of sample items in my application so it doesn't look so empty when the user look at it the first time. The list with the sample items should have an image and the image I am going to use is already stored in the /res/drawable-folder of the application.

因为我已经有了一种从 URI 加载项目图像的方法,所以我想获取到/res/drawable/myImage.jpg 的 URI,但我似乎无法正确处理.

Since I already have a method that load the items images from a URI i'd like to get the URI to the /res/drawable/myImage.jpg but I don't seem to be able to get it right.

流程如下:使用表示图像 URI 的字符串创建项目.将项目列表发送到列表该列表通过将字符串转换为 URL 然后在后台任务中加载图像,然后运行 ​​url.openStream();

The flow is as follows: Create item with string that represents the URI of the image. Send list of items to an List The list loads the image in a background task by converting the string to URL and then run url.openStream();

我为 URI 尝试了几个选项,但都没有成功.android.resource://....."说未知协议找不到file://"文件

I have tried a few options for the URI without any success. "android.resource://....." says unknow protocoll "file://" file not found

所以现在我对如何解决这个问题有点迷茫..

So right now I'm a little bit lost about how to fix this..

推荐答案

您应该使用 ContentResolver 来打开资源 URI:

You should use ContentResolver to open resource URIs:

Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);

您也可以使用此方法打开文件和内容 URI.

Also you can open file and content URIs using this method.

这篇关于获取存储在 drawable 中的图像的 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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