Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Images from assets folder

Tags:

java

android

I have an android application in which I have several images in assets folder. Now I want to make an array of that images. Now my problem is :- when our images are in drawable we can make an array like

int x[] = {
    R.drawable.ss,
    R.drawable.aa, R.drawable.sk,
    R.drawable.xx
};

and so on. how can i make an array of images same as above when my images are in assets folder. I want to make an array at class level.

like image 563
Doctor Who Avatar asked Sep 24 '13 05:09

Doctor Who


2 Answers

You have to read image by image like below:

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.

private Bitmap getBitmapFromAsset(String strName)
    {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }
like image 190
Lokesh Avatar answered Sep 22 '22 20:09

Lokesh


If your images are stored in image folder in assets directory then you can get the list of image as way

private List<String> getImage(Context context) throws IOException {
      AssetManager assetManager = context.getAssets();
      String[] files = assetManager.list("image");   
      List<String> it = Arrays.asList(files);
      return it; 
}
like image 44
Sunil Kumar Avatar answered Sep 22 '22 20:09

Sunil Kumar