Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在棒棒糖上使用Android存储访问框架列出文件时出现错误_Android_Android 5.0 Lollipop_Android Sdcard_Documentfile - Fatal编程技术网

在棒棒糖上使用Android存储访问框架列出文件时出现错误

在棒棒糖上使用Android存储访问框架列出文件时出现错误,android,android-5.0-lollipop,android-sdcard,documentfile,Android,Android 5.0 Lollipop,Android Sdcard,Documentfile,背景 我有一些应用程序大量使用SD卡进行文件同步。Kitkat上坏掉的外部SD卡访问仍然是一个大问题,但我正试图用棒棒糖上提供的新API来解决这个问题 我成功地向SD卡请求并保持权限,并且我可以在从授予权限活动返回的根Uri中列出文件 请参阅有关如何执行此操作的更多信息: 然后,用户可以选择任何文件夹/子文件夹进行同步,我将文件夹文档Uri作为字符串保存在数据库中 问题 稍后,应用程序重新启动后,可能会启动文件同步。然后,我尝试列出子文件夹中的文件(请记住,我已被授予了正确的权限,并且这种持久

背景

我有一些应用程序大量使用SD卡进行文件同步。Kitkat上坏掉的外部SD卡访问仍然是一个大问题,但我正试图用棒棒糖上提供的新API来解决这个问题

我成功地向SD卡请求并保持权限,并且我可以在从授予权限活动返回的根Uri中列出文件

请参阅有关如何执行此操作的更多信息:

然后,用户可以选择任何文件夹/子文件夹进行同步,我将文件夹文档Uri作为字符串保存在数据库中

问题

稍后,应用程序重新启动后,可能会启动文件同步。然后,我尝试列出子文件夹中的文件(请记住,我已被授予了正确的权限,并且这种持久性有效,还授予我访问所有子文件夹的权限)

然后,我从存储的字符串创建DocumentFile的新实例,并尝试列出文件:

  DocumentFile dir = DocumentFile.fromTreeUri(ctx, Uri.parse(storedUri));
  dir.listFiles();
问题是listFiles总是返回授予的根Uri处的子项,而不是我给DocumentFile.fromTreeUri方法的实际Uri的子项

我已经检查了的源代码,似乎有一个bug,特别是我认为没有必要进一步修改Uri:

public static DocumentFile fromTreeUri(Context context, Uri treeUri) {
  final int version = Build.VERSION.SDK_INT;
  if (version >= 21) {
    return new TreeDocumentFile(null, context,
      DocumentsContractApi21.prepareTreeUri(treeUri));
  } else {
  return null;
}
如果我们查看DocumentsContractApi21.prepareTreeUri的源代码,我们会看到它重建了Uri:

 public static Uri prepareTreeUri(Uri treeUri) {
   return DocumentsContract.buildDocumentUriUsingTree(treeUri,
     DocumentsContract.getTreeDocumentId(treeUri));
 }
以及它调用的方法:

 public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) {
   return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
                 .authority(treeUri.getAuthority()).appendPath(PATH_TREE)
                 .appendPath(getTreeDocumentId(treeUri)).appendPath(PATH_DOCUMENT)
                 .appendPath(documentId).build();
 }

 public static String getTreeDocumentId(Uri documentUri) {
   final List<String> paths = documentUri.getPathSegments();
   if (paths.size() >= 2 && PATH_TREE.equals(paths.get(0))) {
     return paths.get(1);
   }
   throw new IllegalArgumentException("Invalid URI: " + documentUri);
 }
publicstaticuribuilddocumenturiusingtree(uritreeuri,stringdocumentid){
返回新的Uri.Builder().scheme(ContentResolver.scheme\u内容)
.authority(treeUri.getAuthority()).appendPath(路径树)
.appendPath(getTreeDocumentId(treeUri)).appendPath(路径\文档)
.appendPath(documentId).build();
}
公共静态字符串getTreeDocumentId(Uri documentUri){
最终列表路径=documentUri.getPathSegments();
如果(PATH.size()>=2&&PATH_TREE.equals(PATH.get(0))){
返回路径。get(1);
}
抛出新的IllegalArgumentException(“无效URI:+documentUri”);
}
getTreeDocumentId找到的文档Id将始终与根Uri Id相对应,无论使用什么Uri调用方法。这使得无法使用提供的框架方法列出子文件夹的子文件夹

解决方案

请修复fromTreeUri方法,使其不总是使用根文档Uri id

执行以下丑陋的黑客程序修复了这个问题,我真的不希望这样

  Class<?> c = Class.forName("android.support.v4.provider.TreeDocumentFile");
  Constructor<?> constructor = c.getDeclaredConstructor(DocumentFile.class, Context.class, Uri.class);
  constructor.setAccessible(true);

  DocumenFile dir = (DocumentFile) constructor.newInstance(null, mCtx, treeUri);
  dir.listFiles();
Class c=Class.forName(“android.support.v4.provider.TreeDocumentFile”);
构造函数=c.getDeclaredConstructor(DocumentFile.class、Context.class、Uri.class);
constructor.setAccessible(true);
DocumenFile dir=(DocumentFile)constructor.newInstance(null,mCtx,treeUri);
dir.listFiles();

潜在的bug似乎已经修复。我也遇到了这个问题,在没有任何代码更改的情况下,它现在可以在
documentfile
包的1.0.1版本中使用

dependencies {
    ...
    implementation 'androidx.documentfile:documentfile:1.0.1'
    ...
}

在b.android.com中有关于这个案例的条目吗?我在android bugtracker中找不到任何条目,但我很乐意在澄清它确实是一个bug之后将其添加到那里,而不仅仅是我做错了什么。我注意到了同样的事情,我也发现它让新的API使用起来非常不舒服。你是否向b.android.com提交了一张罚单?是的,它“工作”与你所描述的一模一样。我认为这是一个特性——当然很奇怪。我使用的方法和你一样。你找到更干净的解决方案了吗?对我有用。Tnx!