joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 772 forks source link

Android: "Permission denied" – no matter what I do #700

Open TheWirv opened 3 years ago

TheWirv commented 3 years ago

Hi, I am trying to save some base64 data to file, but somehow I can't get it to work. Everytime I am trying to access external storage (/storage/emulated/0), I get Permission denied from createFile() or writeFile()—I have tried both already. I am also asking for permission beforehand, as you can see in my code snippet. I also added the stuff to AndroidManifest.xml.

The only location that doesn't raise the Permission denied exception is RNFetchBlob.fs.dirs.DocumentDir, but that is /data/user/0/{bundleId}/files and I am not sure, what that's translating to. This exact path does not exist on my phone, but I suspect it's {Internal storage}/Android/data/{bundleId}/files. Even so, after running createFile()/writeFile() "successfully" there's no file at that location.

Am I missing something crucial here? I'm really out of my depth on this one. I hope someone can help me.

Mousserlane commented 3 years ago

I got similar issue, I tried to write a file to RNFetchBlob.fs.dirs.DownloadDir but it always returns Permisson Denied. After digging a bit more into it turns out Android 10 introduce a new storage paradigm called scoped storage. So far the only workaround that I found is to add

  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>

to your AndroidManifest.xml but this is only temporary. It would be amazing if rn-fetch-blob could support scoped storage 🙇 .

2mt-hillen commented 3 years ago

Got the same Problem, this workaround fixed it for me. Would be great if this will be part of rn-fetch-blob in an next update.

Desintegrator commented 3 years ago

same here

2mt-hillen commented 3 years ago

I found a way to get around it: https://github.com/idealistspace/react-native-android-file-util Use this instead, to save a file somewhere on the phone the other fix doesn't work on Andriod 11

Desintegrator commented 3 years ago

@2mt-hillen "fix doesn't work on Andriod 11" -> you mean already now or its only when app is targeting sdk level 30?

2mt-hillen commented 3 years ago

for us it threw an error on android 11, with the code in the other repo it worked fine

2mt-hillen commented 3 years ago

and we're targeting sdk 29

Findiglay commented 3 years ago

According to the android 11 release notes, requestLegacyExternalStorage should continue to work on Android 11 devices if we are targetting API level 29.

https://developer.android.com/about/versions/11/privacy/storage

Tino-F commented 3 years ago

it's not working on Android 11 devices at all anymore

Desintegrator commented 3 years ago

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Tino-F commented 3 years ago

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below.

https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

Desintegrator commented 3 years ago

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below.

https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

it depends on targetSdkVersion. It should still work if you are targeting API 29

R4DIC4L commented 3 years ago

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below. https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

it depends on targetSdkVersion. It should still work if you are targeting API 29

I can confirm, my app is targeting API 29 and requestLegacyExternalStorage=true is still applying on my Samsung S10+ with Android 11.

anjalsaneen commented 3 years ago
Screenshot 2021-04-22 at 12 54 59 PM

Google won't allow android:requestLegacyExternalStorage after May 5. This needs to be solved soon

matamicen commented 3 years ago

I think is not something about this library, we migrate to API 30, we kept android:requestLegacyExternalStorage=true and add for API 30.

The only thing we have to change in our app is to avoid creating folders because API 30 external storage doesn't allow that. The app is saving to disk without any problem, the only problem we have is that it seems API 30 doesn't allow to rewrite with the same name of the file, any help will be appreciated.

      if (Platform.OS === 'android')
        path = `${RNFetchBlob.fs.dirs.DCIMDir}/videoaux.mp4`;
      else path = `${RNFetchBlob.fs.dirs.DocumentDir}/sqso/videoaux.mp4`;

PS: We didn't publish to the console yet, so we are not 100% sure that "after May 5" will be solved.

Matt

Dror-Bar commented 3 years ago

I got the same message @anjalsaneen linked when trying to update an app. Does anyone have some extra info on this? It seems I was able to upload anyway, but I'm not sure what are the consequences exactly if there's any.

The message says "You must let us know why" - and yet, I didn't see where / how I could let them know why. It also seems like I could add MANAGE_EXTERNAL_STORAGE permission and complete the declaration in the play console as an alternative.

I'm currently targeting API 29, do I need to do any additional action - or is it time to move on to an actual maintained library?

anjalsaneen commented 3 years ago

@Dror-Bar This Google Play policy refers specifically to apps that target API level 30 and need the MANAGE_EXTERNAL_STORAGE permission (All Files Access). If you don’t use or plan to use this permission, this policy shouldn’t affect you. If you are currently targeting API level 29 and want to use this permission when you update to target API level 30, you will need to comply with this policy. The last date to update App to target Sdk 30 is August 2021. have time till August

jalil-ranger commented 3 years ago

This is my build.gradle ext { buildToolsVersion = "29.0.3" minSdkVersion = 23 compileSdkVersion = 30 targetSdkVersion = 29 supportLibVersion = "29.0.0" }

But i still get error. LOG [Error: Permission denied] When i try

if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            this.setState({spinner: true});
                RNFetchBlob.fs.writeFile(path, base64string, 'base64')
                .then((res) => {this.setState({spinner: false});})
                .catch(error => console.log(error));
}

What should i do now.

Secondly is it mandatory to target SDK 30 After August-2021 if so what all should i do

stevefai commented 2 years ago

I can currently access the download directory on all Android versions (including 11) with this setup:

build.gradle:

minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 30

AndroidManifest.xml:

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

I DON'T have the permission MANAGE_EXTERNAL_STORAGE.

I can successfully run RNFetchBlob.fs.exists, RNFetchBlob.fs.writeFile and RNFetchBlob.fs.readFile over the Downloads directory (RNFetchBlob.fs.dirs.DownloadDir), however I don't understand WHY, since the documentation is pretty clear that the MANAGE_EXTERNAL_STORAGE permission is required in order to read and write to the Download directory.

This is what the permissions looks like in the settings: image

Is this ok or will it break at some point or get rejected when submitted in the play store?

codingwatching commented 2 years ago

I can confirm if we add requestLegacyExternalStorage to the application tag then we can read write normally. And yes I wonder if Is this ok or will it break at some point or get rejected when submitted in the play store?

Guilleanto commented 2 years ago

what happen with this bug in android 11?

indraarianggi commented 1 year ago

Is there any progress/solution for this? I just got the same issue on Android 10

alessioemireni commented 1 year ago

you can use https://github.com/RonRadtke/react-native-blob-util that is updated with the implementation of Android Scoped Storage.