My question is specific to Android 11. Since Android 11, we now have concept of scoped storage. I have read many answers on SO but I am still confused.
Few specs of my project:
buildToolsVersion = "29.0.3"
minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 30
ndkVersion = "20.1.5948944"
WHAT I UNDERSTAND FROM SCOPED STORAGE IN ANDROID 11:
Earlier any app can read/write anywhere in the filesystem. Now, an app can read/write only in its private directory (excluding a few 'generally available' directories like DCIM). Is my understanding correct? (Question 1)
Private directory being : Android/data/com.myappname/files/
THERE ARE 2 SCENARIOS IN MY APP:
My app creates a file if its not created and then later reads it. I hardcoded now (for Android 11) the full path in my react native code:
Android/data/com.myappname/files/myfilename
. It is now working fine in android 11. Now my question is: Do I need to mentionREAD/WRITE_EXTERNAL_STORAGE
in AndroidManifest.xml file?(Question 2) Secondly, do I need to ask user using dialog prompt in the app for any (storage) permission? (Question 3)My app is using camera and gallery. For that I have only provided
CAMERA
permission in AndroidManifest.xml. Is it sufficient? What line should I add for reading from gallery? (Question 4)
Permissions from my AndroidManifest.xml is below:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="remove" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" tools:node="remove" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" tools:node="remove" />
<uses-permission android:name="com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE" tools:node="remove" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" tools:node="remove" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" tools:node="remove" />
CodePudding user response:
For app-specific storage you don't need any permission to read and write.
If you want to get some permission from the user it's good to display a dialog as a consent explaining why you need this permission.
For Gallery below android 10 you need the
android.permission READ_EXTERNAL_STORAGE
permission. For the Camera yes you only need to add<uses-permission android:name="android.permission.CAMERA" />
. You can add this line also<uses-feature android:name="android.hardware.camera.any" />
for Play Store.
CodePudding user response:
Yes you need to provide Read, Write and Camera permissions.
In AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
In Info.plist:(Below is just 1 example, you can add any permission you want)
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Need photo library access to upload important documents</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Need photo library access to upload important documents</string>
If you want it programatically in react-native then:
//ANDROID
import {PermissionsAndroid} from 'react-native';
if (Platform.OS === 'android') {
isReadGranted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
);
}
if (isReadGranted === PermissionsAndroid.RESULTS.GRANTED) {
//TODO
}
//IOS
import Permissions, {request} from 'react-native-permissions';
Permissions.check('ios.permission.PHOTO_LIBRARY');//checking for photo library permissions
request('ios.permission.PHOTO_LIBRARY');//requesting for photo lib permission