美文网首页
Android11 内部存储访问权限问题记录

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

作者: 明日未期 | 来源:发表于2021-06-23 08:06 被阅读0次
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 内部存储访问权限问题记录

      本文链接:https://www.haomeiwen.com/subject/ncivyltx.html