react-native-scoped-storage icon indicating copy to clipboard operation
react-native-scoped-storage copied to clipboard

How to ask permission for specific folder

Open EndLess728 opened this issue 3 years ago • 12 comments

I installed one app from playstore and it downloads the whatsapp stories (basically fetches the . statuses folder from internal storage)

When I installed that app, It directly opened WhatsApp/. statuses folder to ask permission for that specific folder

As per new android 11 policy now no app can get third party folder access it need the access from file manager.

I want to ask if there is any method I can ask permission for specific folder. Like WhatsApp or any other folder ?

EndLess728 avatar Feb 07 '22 17:02 EndLess728

any solution looking for this

AftabUfaq avatar Mar 21 '22 17:03 AftabUfaq

any solution looking for this

https://github.com/alpha0010/react-native-file-access/issues/43#issuecomment-1033350666

EndLess728 avatar Mar 21 '22 17:03 EndLess728

@EndLess728 can you provide your code for accessing particular folder , it would be helpful.

sonivaibhav27 avatar Mar 27 '22 09:03 sonivaibhav27

@EndLess728 can you provide your code for accessing particular folder , it would be helpful.

You can get path of specific folder from this command https://docs.expo.dev/versions/latest/sdk/filesystem/#storageaccessframeworkgeturifordirectoryinrootfoldername

And then you can ask permission for that specific folder using this https://docs.expo.dev/versions/latest/sdk/filesystem/#storageaccessframeworkrequestdirectorypermissionsasyncinitialfileurl

These two commands will help you directly open specific folder to ask permission of that specific folder

EndLess728 avatar Mar 27 '22 10:03 EndLess728

Thank you @EndLess728 . I am reading the Uri but when i list the dir file using listFiles getting error as 'content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsapp%2FMedia%2F.Statuses'does not have permission to read/write

sonivaibhav27 avatar Mar 27 '22 10:03 sonivaibhav27

Thank you @EndLess728 . I am reading the Uri but when i list the dir file using listFiles getting error as 'content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsapp%2FMedia%2F.Statuses'does not have permission to read/write

This is the code I am using (dirty code with comments)

const checkStorage = async () => {
    //Example URIs
    const whatsappStatusURI =
      'content://com.android.externalstorage.documents/tree/primary%3AWhatsApp%2FMedia%2F.Statuses';
    const whatsappWallpaperURI =
      'content://com.android.externalstorage.documents/tree/primary%3AWhatsApp%2FMedia%2FWallPaper';

    //-----------------------------------------------------------------//
    const albumUri = StorageAccessFramework.getUriForDirectoryInRoot(
      'WhatsApp%2FMedia%2F.Statuses', //'WhatsApp%2FMedia%2F.Statuses',
    );
    console.log('check album uri', albumUri);

    //-----------------------------------------------------------------//

    // Requests permissions for external directory
    const permissions =
      await StorageAccessFramework.requestDirectoryPermissionsAsync(albumUri);

    if (permissions.granted) {
      // Gets SAF URI from response
      const uri = permissions.directoryUri; //content://com.android.externalstorage.documents/tree/primary%3AWhatsApp%2FMedia%2F.Statuses

      console.log('check uri ---->', uri);
      // Gets all files inside of selected directory
      const files = await StorageAccessFramework.readDirectoryAsync(uri);
      alert(Files inside ${uri}:\n\n${JSON.stringify(files)});
    }
  }

EndLess728 avatar Mar 27 '22 11:03 EndLess728

Hey Thanks you for your code @EndLess728 . I was using Bare React Native Project. I have made changes to openDocumentTree fn native code for android, so that it can now goes directly to specific folder . After changing to code , now i am calling openDocumentTree(IntitialUrl,true). PS: made changes to my local react-native-scoped-storage . NOT PR

sonivaibhav27 avatar Mar 29 '22 13:03 sonivaibhav27

Hey Thanks you for your code @EndLess728 . I was using Bare React Native Project. I have made changes to openDocumentTree fn native code for android, so that it can now goes directly to specific folder . After changing to code , now i am calling openDocumentTree(IntitialUrl,true). PS: made changes to my local react-native-scoped-storage . NOT PR

Glad to know it helped you. Btw can you share the changes you have done ? It will save my time and app size by not using that additional expo system package

EndLess728 avatar Mar 29 '22 14:03 EndLess728

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @ReactMethod
    public void openDocumentTree(String initialFileUrl , final boolean persist, final Promise promise) {

        try {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
      // New Added Start
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Uri fileUri = initialFileUrl == null ? null : Uri.parse(initialFileUrl);
            if (fileUri != null) {
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, fileUri);
            }
        }
     // New Added End
...

sonivaibhav27 avatar Mar 29 '22 14:03 sonivaibhav27

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @ReactMethod
    public void openDocumentTree(String initialFileUrl , final boolean persist, final Promise promise) {

        try {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
      // New Added Start
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Uri fileUri = initialFileUrl == null ? null : Uri.parse(initialFileUrl);
            if (fileUri != null) {
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, fileUri);
            }
        }
     // New Added End
...

I tried your approach, It works for asking permission for specific folder but it doesn't list all the files present in that folder

mantusuff420 avatar Apr 04 '22 06:04 mantusuff420

@mantusuff420 Its Native , so it will handle by OS i.e Android. So Listing Files would not be issue here.

sonivaibhav27 avatar Apr 07 '22 16:04 sonivaibhav27

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @ReactMethod
    public void openDocumentTree(String initialFileUrl , final boolean persist, final Promise promise) {

        try {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
      // New Added Start
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Uri fileUri = initialFileUrl == null ? null : Uri.parse(initialFileUrl);
            if (fileUri != null) {
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, fileUri);
            }
        }
     // New Added End
...

i want to use your method can you please tell me the steps to add this code in my project?

Rohitbarate avatar Jun 19 '23 18:06 Rohitbarate