Android11 内部存储访问权限问题记录

String str="content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fobb";
String str2="content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fobb/document/primary%3AAndroid%2Fobb";

//以下两种uri不可访问,uri与str一致
Uri uri=Uri.parse(str);
Uri uri3=DocumentFile.fromSingleUri(this,uri).getUri();

//以下四种uri可以访问,uri与str2一致
Uri uri2=Uri.parse(str2);
Uri uri4=DocumentFile.fromSingleUri(this,uri2).getUri();
Uri uri5=DocumentFile.fromTreeUri(this,uri).getUri();
Uri uri6=DocumentFile.fromTreeUri(this,uri2).getUri();

遍历文件夹

Uri dirUri=FileUtil.pathToTreeUri(path);
Uri childrenUri= DocumentsContract.buildChildDocumentsUriUsingTree(dirUri, DocumentsContract.getDocumentId(dirUri));
Cursor cursor= getContentResolver().query(childrenUri, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID}, null, null, null);
if (cursor != null) {
    while (cursor.moveToNext()) {
        String documentId = cursor.getString(0);
        Uri uri = DocumentsContract.buildDocumentUriUsingTree(dirUri, documentId);
        DocumentFile f=DocumentFile.fromSingleUri(getContext(), uri);
    }
}

你可能感兴趣的:(Android11 内部存储访问权限问题记录)