Android DocumentFile无效的URI

7

我正在尝试使用DocumentFile在我的Android 5.1手机上列出外部存储设备中的文件。

String rootPathURI = "file:/media/storage/sdcard1/data/example.externalstorage/files/";


File f = new File(URI(rootPathURI));
DocumentFile documentFile = DocumentFile.fromFile(f);

这段代码可行,但我想按照以下方式处理:
String rootPathURI = "file:/media/storage/sdcard1/data/example.externalstorage/files/";

DocumentFile documentFile = DocumentFile.fromTreeUri(getApplicationContext(), Uri.parse(rootPathURI));

但我遇到了这样的异常:
W/System.err( 5157): java.lang.IllegalArgumentException: Invalid     URI:"file:/media/storage/sdcard1/data/example.externalstorage/files/"
2个回答

3

fromTreeUri() 方法用于处理从 ACTION_OPEN_DOCUMENT_TREE 请求中返回的 Uri,参见文档。它也可能适用于 StorageVolume Uri。


你可以给我一个 StoreVolume Uri 的例子吗? - Tom

2
如果您不想使用ACTION_OPEN_DOCUMENT_TREE或ACTION_OPEN_DOCUMENT来获取Uri,您可以使用以下方法将FILE转换为Uri(SAF),该方法从API19(Android4.4-Kitkat)到API28(Android8-Oreo)有效。返回的Uri与对话框返回的Uri相同,并且对于API 28安全限制(SAF权限)是有效的,如果您想访问应用程序外部可移动存储设备...
 /**
 * Ing.N.Nyerges 2019 V2.0
 *
 * Storage Access Framework(SAF) Uri's creator from File (java.IO),
 * for removable external storages
 *
 * @param context Application Context
 * @param file File path + file name
 * @return Uri[]:
 *   uri[0] = SAF TREE Uri
 *   uri[1] = SAF DOCUMENT Uri
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static Uri[] getSafUris (Context context, File file) {

    Uri[] uri = new Uri[2];
    String scheme = "content";
    String authority = "com.android.externalstorage.documents";

    // Separate each element of the File path
    // File format: "/storage/XXXX-XXXX/sub-folder1/sub-folder2..../filename"
    // (XXXX-XXXX is external removable number
    String[] ele = file.getPath().split(File.separator);
    //  ele[0] = not used (empty)
    //  ele[1] = not used (storage name)
    //  ele[2] = storage number
    //  ele[3 to (n-1)] = folders
    //  ele[n] = file name

    // Construct folders strings using SAF format
    StringBuilder folders = new StringBuilder();
    if (ele.length > 4) {
        folders.append(ele[3]);
        for (int i = 4; i < ele.length - 1; ++i) folders.append("%2F").append(ele[i]);
    }

    String common = ele[2] + "%3A" + folders.toString();

    // Construct TREE Uri
    Uri.Builder builder = new Uri.Builder();
    builder.scheme(scheme);
    builder.authority(authority);
    builder.encodedPath("/tree/" + common);
    uri[0] = builder.build();

    // Construct DOCUMENT Uri
    builder = new Uri.Builder();
    builder.scheme(scheme);
    builder.authority(authority);
    if (ele.length > 4) common = common + "%2F";
    builder.encodedPath("/document/" + common + file.getName());
    uri[1] = builder.build();

    return uri;
}

您必须使用SAF权限来授予访问外部可移动存储的权限,类似于以下内容:

    context.grantUriPermission(context.getPackageName(), uri, Intent
            .FLAG_GRANT_READ_URI_PERMISSION | Intent
            .FLAG_GRANT_WRITE_URI_PERMISSION);
    context.getContentResolver().takePersistableUriPermission(uri, Intent
            .FLAG_GRANT_READ_URI_PERMISSION | Intent
            .FLAG_GRANT_WRITE_URI_PERMISSION);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接