Looping a method many times causes error

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 17:21:54

问题


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 method which is being looped.

 public  static DocumentFile documentfile(final File file ) {

    for (UriPermission permissionUri :con.getContentResolver().getPersistedUriPermissions()) {

        DocumentFile rootDocFile = DocumentFile.fromTreeUri(con, permissionUri.getUri());

        String[] parts = (file.getPath()).split("\\/");

        for (int i = 3; i < parts.length; i++) {

            if (rootDocFile != null)
            {
                rootDocFile = rootDocFile.findFile(parts[i]);
            }

            else {

                rootDocFile = DocumentFile.fromTreeUri(con,permissionUri.getUri() );
                rootDocFile = rootDocFile.findFile(parts[i]);
            }

        }


        return rootDocFile;

    }
    return null;
}

This is how I am looping the method

for(int i=0;i<size;i++)
{
    documentfile(file).createFile(mime, name)
}

All the above code is being executed inside an Async Task.

Any help would be really Grateful.

EDIT: Tried with the Updated code but still received the same error.

Updated Code

public static DocumentFile DocumentFile(final File file)
{

    DocumentFile rootDocFile = DocumentFile.fromTreeUri(con, permission().getUri());

    String[] parts = (file.getPath()).split("\\/");

    for (int i = 3; i < parts.length; i++)
    {

        rootDocFile = rootDocFile.findFile(parts[i]);

    }
    return rootDocFile;
}

public static UriPermission permission()
{
    for (UriPermission permissionUri : con.getContentResolver().getPersistedUriPermissions())
    {
        final File uri_path = new File(FileUtil.getFullPathFromTreeUri(permissionUri.getUri(), con));

        if (uri_path.getName().toLowerCase().equals(new File("SD_CARD_PATH").getName().toLowerCase()))
        {
            return permissionUri;

        }

    }

    return null;
}

This is how I am checking if the permission granted is for SD Card or not

public static boolean wrong_directory_selected(Uri uri, Context con)
    {

        final File uri_path=new File(FileUtil.getFullPathFromTreeUri(uri,con));
        if(uri_path.getName().toLowerCase().equals(new File("SD CARD PATH").getName().toLowerCase()))
        {

            return false;
        }
        return  true;
    }

回答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.

public static DocumentFile documentfile(final File file ) {   
  for (UriPermission permissionUri : con.getContentResolver().getPersistedUriPermissions()) {   
        DocumentFile rootDocFile = DocumentFile.fromTreeUri(con, permissionUri.getUri());    
        String[] parts = (file.getPath()).split("/");    
          for (int i = 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



来源:https://stackoverflow.com/questions/45263806/looping-a-method-many-times-causes-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!