Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 565
kishidp Avatar asked Jul 31 '12 07:07

kishidp


1 Answers

Checkout this code . IN this tutorial you can find how to load image from asset folder.

// load image

try  {     // get input stream     InputStream ims = getAssets().open("avatar.jpg");     // load image as Drawable     Drawable d = Drawable.createFromStream(ims, null);     // set image to ImageView     mImage.setImageDrawable(d);     ims .close(); } catch(IOException ex)  {     return; } 
like image 96
Chirag Avatar answered Oct 21 '22 04:10

Chirag