Selman ALPDÜNDAR

How to read all image from sub folder of assets in android

Create new project and add an imageswitcher to activity_main.xml then open MainActivity.java firstly we need to reach assets folder in android to reach assets folder there is an assets manager basicly you can use assets manager like that

 
 AssetManager assetManager = getAssets();
/* this will read main folder of assets 
if you need read sub folder 
just add folder name before file (subfolder/filename)
*/
 InputStream inputStream= assetManager.open("File name");

We want to read all file from sub folder of assets so we need a list so we will create list. Firstly, I will create a list of file as a string array and calculate count of file in folder to create a loop then,I will create a drawable array to keep all image in it.

     try {

           // to reach asset
          AssetManager assetManager = getAssets();
          // to get all item in dogs folder.
          String[] images = assetManager.list("dogs");
          // to keep all image 
          Drawable[] drawables = new Drawable[images.length];
             // the loop read all image in dogs folder and  aa
            for (int i = 0; i < images.length; i++) {
                inputStream = getAssets().open("dogs/" + images[i]);
                Drawable drawable = Drawable.createFromStream(inputStream, null);
                 drawables[i] = drawable;
            }
        } catch (IOException e) {
                // you can print error or log.
        }

I have read all image  and add all of them an imageswitcher and to swipe I add two image button then the project was finished I will give you source code of this project below.

End of Project

Github 



4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.