Load all images from assets folder dynamically

删除回忆录丶 提交于 2019-12-03 14:22:50

Is there any way that i can create a List or an ArrayList with all the images from the assets subfolder??

Yes, create sub-folder in assets directory. use getAssets().list(<folder_name>) for getting all file names from assets:

String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));

Now to set image in imageview you first need to get bitmap using image name from assets :

InputStream inputstream=mContext.getAssets().open("images/"
                                      +listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);

String[] images =getAssets().list("images"); returns the content of folder "images" in assetes.

If your assets folder directly contains images them u should use

String[] images =getAssets().list("");
public static ArrayList<String> addAssetsImages(Context mContext, String folderPath) {
        ArrayList<String> pathList = new ArrayList<>();
        try {
            String[] files = mContext.getAssets().list(folderPath);
            for (String name : files) {
                pathList.add(folderPath + File.separator + name);
                Log.e("pathList item", folderPath + File.separator + name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pathList;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!