kishidp kishidp - 11 days ago 6
Android Question

Load an image from assets folder

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.

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)

thanks in advance.

EDIT

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

Answer

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);

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