Android Q(10)要求获得访问所有存储的权限.范围存储 [英] Android Q (10) ask permission to get access all storage. Scoped storage

查看:82
本文介绍了Android Q(10)要求获得访问所有存储的权限.范围存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在需要访问存储的Android 10应用中,需要征求访问特定路径的权限.但是,诸如文件浏览器之类的应用程序可以请求访问根存储的权限,并获得读取/写入所有存储的权限.那就是我想要做的.

In Android 10 apps that need to access storage need to ask permission to access a concrete path. But apps like file explorer can ask permission for access to the root storage and gain permission to read/write to all storage. That is what I am trying to do.

根据Android,我们必须为此使用 ACTION_OPEN_DOCUMENT_TREE .我的问题是,一切似乎都正确,但是未将权限授予该应用.

According to Android we must use ACTION_OPEN_DOCUMENT_TREE for this. The problem I have is that everything seems correct, but the permission is not granted to the app.

  private void askAndroid10Perm()
    {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.addFlags(
                Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                        | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                        | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
        startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE);
    }

在此用户可以看到Android文件树,并在选择了主存储的情况下单击以授予权限.它将允许-应用程序名称-可以完全访问当前存储在此位置下的所有文件,以及将来存储在此处的所有内容"->允许

Here the user can see Android file tree and, with main storage selected, click to grant permission. "It will allow - app name - to have full access to all files currently store under this location, and any future content stored here" -> Allow

然后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQUEST_CODE_OPEN_DOCUMENT_TREE:
            if (resultCode == Activity.RESULT_OK) {
                Uri treeUri = data.getData();
                int takeFlags = data.getFlags();
                takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                    Log.i("TAG", "takePersistableUriPermission: " + treeUri);
                    this.getContentResolver().takePersistableUriPermission(treeUri, takeFlags);

                }

            }
    }
}

日志:

takePersistableUriPermission:content://com.android.externalstorage.documents/tree/primary%3A

takePersistableUriPermission: content://com.android.externalstorage.documents/tree/primary%3A

然后,该应用仍将无权访问root.

Then the app will still not have permission to access to root.

打开失败:EACCES(权限被拒绝)

open failed: EACCES (Permission denied)

我当然有:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

由用户授予.

推荐答案

范围存储不被PlayConsole接受,因此现在无法使用.

Scoped Storage not accept by PlayConsole so now its not Worked.

适用于Android 11或更高版本的Work.

For Work Android 11 or Up.

在Android Q中,如果您授予权限及其授予或不起作用,请转到清单"并添加

in Android Q If you giving permission and its granted or not working so go to Manifest and add

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:requestLegacyExternalStorage="true" //Add this Line
    android:label="@string/app_name">

---------<Activity, Sevices or Recivers>----------

</application>

以及在build.gradle

and in build.gradle

compileSdkVersion 29 //SDK Version don't update to 30
defaultConfig {
    applicationId 'com.example.application'
    minSdkVersion 16
    targetSdkVersion 29 //SDK Version don't update to 30
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

这篇关于Android Q(10)要求获得访问所有存储的权限.范围存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆