Load an image from assets folder Load an image from assets folder android android

Load an image from assets folder


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


Here you are,

  public Bitmap getBitmapFromAssets(String fileName) throws IOException {    AssetManager assetManager = getAssets();    InputStream istr = assetManager.open(fileName);    Bitmap bitmap = BitmapFactory.decodeStream(istr);    istr.close();    return bitmap;}


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.