0

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?

Neha Soni
  • 3,935
  • 2
  • 10
  • 32
  • Can you mention device detail in which you are checking this .. like OS of that device ? If you are checking on newer android os then you will need to take care as new android version have added some restriction for that https://developer.android.com/about/versions/11/privacy/storage – jayesh gurudayalani Apr 27 '23 at 04:53
  • Hey, thanks for the comment. I added the device details to the question. Please check. @jayeshgurudayalani – Neha Soni Apr 27 '23 at 05:33
  • Got it .. As you are using it in 33 so you will have to add condition to skip to check storage permission if api level is >= 33 . and write any file to your app private directory only as it will not require to ask WRITE_EXTERNAL_STORAGE permission Please check this answer https://stackoverflow.com/a/74296799/5696047 – jayesh gurudayalani Apr 27 '23 at 05:48
  • So, it means, I don't need to ask for permission at all if I am targeting new android devices? And would this be same in case of READ_EXTERNAL_STORAGE? – Neha Soni Apr 27 '23 at 05:59
  • Well for reading , different permission are introduced which you can check here https://developer.android.com/training/data-storage/shared/media – jayesh gurudayalani Apr 27 '23 at 06:02
  • Thank you so much. I checked if `Platform.Version >= 33`, skip request permission and it worked. Please post your knowledge as an answer for future reference. – Neha Soni Apr 27 '23 at 06:06

1 Answers1

2

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

jayesh gurudayalani
  • 1,368
  • 1
  • 9
  • 14