Iman Salehi
Iman Salehi

Reputation: 956

cant access camera roll in photos react native

I want to access the phone camera roll photos, in React-native by this code:

_handleButtonPress = () => {

    CameraRoll.getPhotos({
        first: 20,
        assetType: 'Photos',
    })
        .then(r => {
            this.setState({ photos: r.edges });
        })
        .catch((err) => {
            alert(err)
        });
};

but i get this Error in alert:

could not get photos need: read_external_storage permission

Upvotes: 1

Views: 3173

Answers (2)

Iman Salehi
Iman Salehi

Reputation: 956

i added this code in AndroidManifest located at(( Example/android/app/src/main/AndroidManifest.xml)):

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

then i confirmed app permissions in android(and i dont know how to do it at run time) and problem solved.

Upvotes: 0

Pritish Vaidya
Pritish Vaidya

Reputation: 22209

As mentioned in the Google Docs, if the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time.

Therefore you must ask the user to grant the dangerous permissions at runtime

Here's a list of Dangerous Permissions.

You can enable the permissions in React Native as

try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
      {
        'title': 'Access Storage',
        'message': 'Access Storage for the pictures'
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use read from the storage")
    } else {
      console.log("Storage permission denied")
    }
  } catch (err) {
    console.warn(err)
  }

Check here for more details

Upvotes: 3

Related Questions