Reputation: 112
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.
Upvotes: 0
Views: 1845
Reputation: 3154
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);
}
}
Upvotes: 3