从资产文件夹加载图像 [英] Load an image from assets folder

查看:55
本文介绍了从资产文件夹加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 asset 文件夹加载图像,然后将其设置为 ImageView.我知道如果我为此使用 R.id.* 会好得多,但前提是我不知道图像的 id.基本上,我试图通过其文件名动态加载图像.

I am trying to load an image from the asset folder and then set it to an ImageView. I know it's much better if I use the R.id.* for this, but the premise is I don't know the id of the image. Basically, I'm trying to dynamically load the image via its filename.

例如,我在 database 中随机检索了一个元素,它代表了一个 'cow',现在我的应用程序要做的是显示一个 'cow' 的图像em>'cow' 通过 ImageView.对于 database 中的所有元素也是如此.(假设是,对于每个元素都有一个等效的图像)

For example, I randomly retrieve an element in the database representing let's say a 'cow', now what my application would do is to display an image of a 'cow' via the ImageView. This is also true for all element in the database. (The assumption is, for every element there is an equivalent image)

提前致谢.

编辑

忘记问题了,如何从 asset 文件夹中加载图像?

forgot the question, how do I load the image from the asset folder?

推荐答案

如果你知道代码中的文件名,调用这个就不会有问题:

If you know the filename in the code, calling this won't be a problem:

ImageView iw= (ImageView)findViewById(R.id.imageView1);  
int resID = getResources().getIdentifier(drawableName, "drawable",  getPackageName());
iw.setImageResource(resID);

您的文件名将与 drawableName 同名,因此您不必处理资产.

Your filename will be the same name as drawableName so you won't have to deal with assets.

这篇关于从资产文件夹加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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