Android Q (10) ask permission to get access all storage. Scoped storage

核能气质少年 提交于 2020-03-21 16:20:51

问题


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

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 finally 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);
    }

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

Then:

@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);

                }

            }
    }
}

Log:

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

Then the app have not permission to access to root.

open failed: EACCES (Permission denied)

Of curse I have

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

Garented by user.


回答1:


The problem was that I use File object to access files, but permission only work if we use DocumentFile object to manipulate files.

Solved using DocumentFile instead of File.

It has multiple negative points to use DocumentFile, such as performance and that you don't have access to apache commons libraries for example, but there is no other option.




回答2:


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>


来源:https://stackoverflow.com/questions/57449242/android-q-10-ask-permission-to-get-access-all-storage-scoped-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!