多次循环方法会导致错误 [英] Looping a method many times causes error

查看:37
本文介绍了多次循环方法会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环一个方法,该方法将DocumentFile返回100次甚至更多次.很多时候该方法返回null,而它已经被循环了35次左右.

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.

这是正在循环的方法.

 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.

编辑:尝试使用更新的代码,但仍然收到相同的错误.

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

更新代码

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;
}

这是我检查授予的权限是否适用于SD卡的方式

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;
    }

推荐答案

在代码的 else 部分内部,并且在第一行中,您正在创建一个新的 DocumentFile 仅从 sd-card 的URI,然后在第二行,您尝试在 sd-card 的根目录中找到一个文件,该文件没有任何作用.

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.

在您从用户提供的URI中获取空值之后,我无法猜测else部分背后的逻辑.

I can't guess the logic behind your else part after you getting null out of the user`s provided URI.

当您从这种方法中获得空值时,这意味着用户被错误选择为 sd-card 目录.因此,您要做的第一件事就是要求用户提供 sd卡的正确路径.

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;
}

如果 rootDocFile 为空,则要求用户提供正确的路径

If rootDocFile is null then ask the user for the correct path

此处

这篇关于多次循环方法会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆