I have one problem I know how to check if folder is empty, but I always get an error when I check if folder is empty in loop, here is example of my code which doesn’t work.
String sdcard = Environment.getExternalStorageDirectory().toString();
File file = new File(sdcard);
File subFiles[] = file.listFiles();
for(int i=0; i<subFiles.length; i++) {
File subFile = new File(sdcard +"/"+ subFiles[i].getName());
if(subFile.isDirectory() && subFile.listFiles().length==0) {
}
}
I always get the error in my condition.
Here’s a better version of your code, it’s a recursive one also, means it will search for all the empty folders on the storage. Actually, the null check is not a right thing to make here, cause the documentation tells that
nullwill only be returned iffileis not a directory.Let me know if this works.