ammarahm-ed / react-native-scoped-storage

MIT License
58 stars 10 forks source link

How to ask permission for specific folder #25

Open EndLess728 opened 2 years ago

EndLess728 commented 2 years ago

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 ?

AftabUfaq commented 2 years ago

any solution looking for this

EndLess728 commented 2 years ago

any solution looking for this

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

sonivaibhav27 commented 2 years ago

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

EndLess728 commented 2 years ago

@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

sonivaibhav27 commented 2 years ago

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

EndLess728 commented 2 years ago

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)});
    }
  }
sonivaibhav27 commented 2 years ago

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

EndLess728 commented 2 years ago

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

sonivaibhav27 commented 2 years ago
    @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
...
mantusuff420 commented 2 years ago
    @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

sonivaibhav27 commented 2 years ago

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

Rohitbarate commented 1 year ago
    @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?