Search code examples
androidreactjsreact-nativeandroid-permissionsreact-native-permissions

WRITE_EXTERNAL_STORAGE permission is always blocked in React Native android platform


App environment details-

"react": "18.2.0",
"react-native": "0.71.7",
"react-native-permissions": "^3.8.0",
"react-native-webview": "^12.0.2"

Testing device details-

Name - sdk_gphone64_x86_64
SDK version - 33
Android version- 13

I want to add WRITE_EXTERNAL_STORAGE permission in my app and therefore I made these changes-

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:roundIcon="@mipmap/ic_launcher"
  android:allowBackup="true"
  android:requestLegacyExternalStorage="true"
  android:theme="@style/AppTheme"
>
...
</application>

But when I install the app, it is not asking permission to write data to external storage. Also, when I check the App's configuration, only Camera permission is listed.

Inside my JS code when I check and request this permission, the response is always blocked.

// INITIALLY THIS FUNCTION GETS CALLED ON CLICK OF SOME ACTION
async checkPermission(permissionType: any) {
  // permissionType is "WRITE_EXTERNAL_STORAGE"
  await request(
    Platform.select({
      android: PERMISSIONS.ANDROID[permissionType],
    }),
  ).then((response: string) => {
    if (response !== 'authorized') {
      // CONTROL GOES HERE
      this.requestPermissions(permissionType);
    }
  });
}

async requestPermissions(permissionType: any) {
  await request(
    Platform.select({
      android: PERMISSIONS.ANDROID[permissionType],
    }),
  ).then(response => {
    if (['restricted', 'blocked', 'never_ask_again'].includes(response)) {
      // CONTROL GOES HERE BECAUSE RESPONSE IS ALWAYS BLOCKED
    }
  });
}

I don't understand why storage permission is not working. Is something changed in React native new version?


Solution

  • Well as you are checking your code in API level 33 (Android 13) , you have to skip to check and ask permission for WRITE_EXTERNAL_STORAGE from api level 33 as new android versions have introduced restrictions for storage access and provide secure way to access data . It is suggested always to write your data to app directory only(cache directory) if it not necessary to show data to user . For more information related changes of storage , go thought this documentation

    Links:-

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

    https://developer.android.com/training/data-storage/shared/media

    https://stackoverflow.com/a/74296799/5696047