Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Permissions not granted... react-native-image-picker

I am trying to upload an image from internal memory in our client app, using

react-native-image-picker@^0.26.4

And after executing the following sample snippet i got the response in an unexpected way.

ImagePicker.showImagePicker(options, response => {
  console.log("Response = ", response)

  if (response.didCancel) {
    console.log("User cancelled image picker")
  } else if (response.error) {
    console.log("ImagePicker Error: ", response.error)
  } else if (response.customButton) {
    console.log("User tapped custom button: ", response.customButton)
  } else {
    const source = { uri: response.uri }
    // RNGRP.getRealPathFromURI(response.uri).then(filePath => {
    //   uploadImageToS3(filePath, "dinesh")
    //   console.log(filePath)
    // })

    // You can also display  the image using data:
    // let source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      avatarSource: source
    })
  }

RESPONSE

  • Response = {error: "Permissions weren't granted"} error: "Permissions weren't granted"proto: Object D:\my_app\index.js:124 ImagePicker Error: Permissions weren't granted

  • Sometimes On Allow button press app unexpectedly crashes.

Additional Information

  • React Native version: ~0.46.1
  • React: 16.0.0-alpha.12
  • Platform: [Android 5.1 and above]
  • Development Operating System: [Windows 7 Professional]
  • Dev tools: [ Android Studio version 2.3.2, Android SDK 23]
like image 761
Pavan Gangireddy Avatar asked Aug 16 '17 10:08

Pavan Gangireddy


3 Answers

Add this code in your AndroidManifest.xml:

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

Edited this after suggestions by Jaffar Raza and szskdgi

like image 85
Zafar Kurbonov Avatar answered Nov 20 '22 13:11

Zafar Kurbonov


Add this code in the android/app/src/main/AndroidManifiest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
like image 29
Malsha Madushani Avatar answered Nov 20 '22 14:11

Malsha Madushani


after adding the following permission to AndroidManifiest.xml

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

we should have checked the permission before processing the functionality.

ex :- step 1(check permission whether it's GRANTED or not)

const isCameraAuthorized = await permissions.checkPermission(
          PermissionType.CAMERA
        );
enter code here

step 2(request permission)

 cameraPermissionStatus = await permissions.requestPermission(
            PermissionType.CAMERA
          );

then we have do the same for WRITE_EXTERNAL_STORAGE as

cameraPermissionStatus = await permissions.requestPermission(
                PermissionType.WRITE_EXTERNAL_STORAGE 
              );
 const isCameraAuthorized = await permissions.checkPermission(
              PermissionType.WRITE_EXTERNAL_STORAGE 
            );
like image 1
chamara bandara Avatar answered Nov 20 '22 13:11

chamara bandara