简体   繁体   中英

Load image files from internal storage into arrayList

I am busy developing an Android application for university and would like to load all images saved into an internal storage folder into an array list. I have managed to do it using assets but I need to be able to do it using the image files on the internal storage folder that I created

Code used to create the folder

File root_text = Environment.getExternalStorageDirectory();
try {
    File folder = new File(Environment.getExternalStorageDirectory() + "/InkousticImages");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
}

Current code used to load asset files into an array list and open them into an inputStream(this is what I want to do but with the files from the internal storage instead)

String[] imageList = getAssets().list("images");
List <String> myList = new ArrayList<>();
for (String filename: imageList) {
    if (filename.toLowerCase().endsWith(".jpg") || filename.toLowerCase().endsWith("jpeg")) {
        myList.add(filename);
    }
}
AssetManager assetManager = getAssets()
InputStream istr = assetManager.open("images/" + myList.toArray()[index]);

Any help would be greatly appreciated.

Here, I wrote the code for you, This loops over all the files in the directory and checks if the extension is a valid image and adds the name to the List

List<String> fileNames = new ArrayList<>();
File folder = new File(Environment.getExternalStorageDirectory(), "InkousticImages");
if (!folder.exists()) folder.mkdir();
for (File file : folder.listFiles()) {
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".jpg") || filename.endsWith("jpeg")) {
        fileNames.add(filename);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM