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?


Solution 1:

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

Solution 2:

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

Solution 3:

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.

Solution 4:

Some of these answers may answer the question but I never liked any of them so I ended up writing this, it my help the community.

Get Bitmap from assets:

public Bitmap loadBitmapFromAssets(Context context, String path)
{
    InputStream stream = null;
    try
    {
        stream = context.getAssets().open(path);
        return BitmapFactory.decodeStream(stream);
    }
    catch (Exception ignored) {} finally
    {
        try
        {
            if(stream != null)
            {
                stream.close();
            }
        } catch (Exception ignored) {}
    }
    return null;
}

Get Drawable from assets:

public Drawable loadDrawableFromAssets(Context context, String path)
{
    InputStream stream = null;
    try
    {
        stream = context.getAssets().open(path);
        return Drawable.createFromStream(stream, null);
    }
    catch (Exception ignored) {} finally
    {
        try
        {
            if(stream != null)
            {
                stream.close();
            }
        } catch (Exception ignored) {}
    }
    return null;
}

Solution 5:

According to Android Developer Documentation loading with bitmap can degrade app performane.Here's a link! So doc suggest to use Glide library.

If you want to load image from assets folder then using Glide library help you alots easier.

just add dependencies to build.gradle (Module:app) from https://github.com/bumptech/glide

 dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

sample example :

// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
}

In case not worked by above method : Replace this object with view object from below code (only if you have Inflate method applied as below in your code).

 LayoutInflater mInflater =  LayoutInflater.from(mContext);
        view  = mInflater.inflate(R.layout.book,parent,false);