Answer the question
In order to leave comments, you need to log in
How to select only one image from a folder?
I am writing a gallery for the application. Here, with the help of this function, I recursively go through all the directories and look for pictures there
private ArrayList<Album> getAlbums(File root){
ArrayList<Album> albums = new ArrayList<>();
String androidDataFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data";
File[] fileList = root.listFiles();
String exts = "jpg png jpeg";
String file_ext;
for(File f: fileList){
if(f.isDirectory() && !f.isHidden()
&& !f.getAbsolutePath().equals(androidDataFolder)) {
albums.addAll(getAlbums(f));
}
else {
file_ext = f.getName().substring(f.getName().lastIndexOf(".") + 1);
if(exts.contains(file_ext)) {
albums.add(new Album(f.getParent(), f));
}
}
}
return albums;
}
private ArrayList<Album> clearAlbums(ArrayList<Album> albums){
boolean isUnique = true;
ArrayList<Album> cleanData = new ArrayList<>();
for (Album a: albums){
for (Album b: cleanData){
isUnique &= !a.getAlbumTitle().equals(b.getAlbumTitle());
}
if(isUnique)
cleanData.add(a);
isUnique = true;
}
return cleanData;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question