负荷资产文件夹中的图像负荷、图像、资产、文件

2023-09-04 06:11:17 作者:生活不易靠演技.

我试图加载从资产文件夹中的图像,然后将其设置为一个的ImageView 。我知道这是好多了,如果我使用了 R.id。* 这一点,但在premise是我不知道图像的编号。基本上,我试图通过它的文件名来动态加载图像。

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.

例如,笔者随机检索的数据库中的元素重presenting让我们说的牛的,现在该怎么办我的应用程序会做的是通过的ImageView 来显示的牛的的图像。这也适用于在数据库的所有元素。 (这里的假设是,对于每一个元件有一个等效图像)

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)

在此先感谢。

修改

忘了这个问题,我怎么从资产加载图像文件夹?

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

推荐答案

如果您知道在code中的文件名,称这不会是一个问题:

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.