从资产文件夹加载图像

从资产文件夹加载图像

问题描述:

我正在尝试从 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.