Android Q上的安全异常:UID XXXXX没有权限访问content://com.android.externalstorage.documents/tree/downloads [user 0]。
创始人
2024-08-15 03:00:14
0

在Android Q上,访问外部存储的权限限制更加严格。为了解决这个问题,您需要请求适当的权限并使用正确的方式访问外部存储。

以下是一种可能的解决方法:

  1. 在AndroidManifest.xml文件中添加以下权限:

  1. 在需要访问外部存储的地方,请求这个权限:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            REQUEST_PERMISSION_READ_EXTERNAL_STORAGE);
}
  1. 处理权限请求的结果,在Activity的onRequestPermissionsResult方法中添加以下代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_PERMISSION_READ_EXTERNAL_STORAGE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 权限已授予,可以访问外部存储
        } else {
            // 权限被拒绝,无法访问外部存储
        }
    }
}
  1. 使用正确的方式访问外部存储。在Android Q上,您需要使用Storage Access Framework(SAF)来访问外部存储。以下是一个使用SAF来获取下载目录的示例代码:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
        DocumentsContract.buildRootsUri("com.android.externalstorage.documents"));
startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE);

在onActivityResult中处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_OPEN_DOCUMENT_TREE && resultCode == RESULT_OK) {
        Uri treeUri = data.getData();
        getContentResolver().takePersistableUriPermission(treeUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        // 使用treeUri访问外部存储
    }
}

通过以上步骤,您应该能够在Android Q上成功访问外部存储。请注意,这只是一个示例解决方案,您可能需要根据您的具体需求进行适当的调整和修改。

相关内容

热门资讯

透视肯定!hhpoker软件安... 透视肯定!hhpoker软件安装包,hhpkoer辅助器,可靠技巧(有挂介绍)所有人都在同一条线上,...
透视ai!hhpoker怎么破... 透视ai!hhpoker怎么破解,hhpoker真能买到挂吗,德州论坛(有挂细节);1、每一步都需要...
透视科技!hhpoker辅助软... 透视科技!hhpoker辅助软件,hhpoker外挂靠谱吗,2025新版教程(有挂揭秘);1、进入到...
透视黑科技!hhpkoer辅助... 透视黑科技!hhpkoer辅助挂是真的吗,hhpoker开挂教程,扑克教程(有挂工具)1、透视黑科技...
透视透视!hhpoker买挂,... 透视透视!hhpoker买挂,hhpoker德州有挂吗,靠谱教程(有挂解密)1、hhpoker德州有...
透视免费!hhpoker可以控... 透视免费!hhpoker可以控制牌吗,hhpoker底牌透视脚本,扑克教程(有挂详情)1、hhpok...
透视辅助!hhpoker一直输... 透视辅助!hhpoker一直输有挂吗,hhpoker软件安装包,新版2025教程(有挂细节)1、玩家...
透视规律!hhpoker智能辅... 透视规律!hhpoker智能辅助插件,hh poker插件下载,靠谱教程(有挂介绍)1、构建自己的h...
透视肯定!德州局HHpoker... 透视肯定!德州局HHpoker透视脚本,hhpoker德州有挂吗,可靠教程(有挂黑科技);1、每一步...
透视透视!hhpoker可以控... 透视透视!hhpoker可以控制吗,hhpoker万能辅助器,2025新版教程(有挂插件);1、hh...