Looping A Method Many Times Causes Error
I am looping a method which returns DocumentFile for 100 or even more times . Many times the method returns null while it has already been looped for around 35 times. This is the m
Solution 1:
Inside the else
part of your code and on the first line, you are creating a new DocumentFile
just from the sd-card
's URI and then on the second line, you try to find a file on the root directory of the sd-card
which provides you nothing.
I can't guess the logic behind your else part after you getting null out of the user`s provided URI.
When you get null from this approach it means that the user has been selected a wrong directory as the sd-card
.
So the first thing you have to do is to ask the user to provide the correct path to the sd-card
.
publicstatic DocumentFile documentfile(final File file ) {
for (UriPermission permissionUri : con.getContentResolver().getPersistedUriPermissions()) {
DocumentFilerootDocFile= DocumentFile.fromTreeUri(con, permissionUri.getUri());
String[] parts = (file.getPath()).split("/");
for (inti=3; i < parts.length; i++) {
if (rootDocFile != null) {
rootDocFile = rootDocFile.findFile(parts[i]);
}
if (rootDocFile != null) {
break;
}
//else {// rootDocFile = DocumentFile.fromTreeUri(con, permissionUri.getUri());// rootDocFile = rootDocFile.findFile(parts[i]);//}
}
}
return rootDocFile;
}
If rootDocFile
is null then ask the user for the correct path
See my previous explanation here
Post a Comment for "Looping A Method Many Times Causes Error"