Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing camera roll permission in expo

I have "Change Image" button and I want to user camera roll to change the image.
But I get warning that I don't have permission to use camera roll.
How do I check if permission is granted or not? If it is not I want to ask for permission.

This is my code for now:

    _pickImage = async () => {
        let result = await ImagePicker.launchImageLibraryAsync({
          allowsEditing: true,
          aspect: [4, 3],
        });
    
        console.log(result);
    
        if (!result.cancelled) {
          this.setState({ image: result.uri });
        }
    };

This may be very dumb question but I am bit confused here...

If you need any more information please comment.

Thanks!

like image 276
crodev Avatar asked Aug 22 '18 11:08

crodev


1 Answers

If you are using Expo you can get Permission from Expo. follow their docs, its great!

It would look something like this:

import * as Permissions from 'expo-permissions';

async componentDidMount() {
  const permission = await Permissions.getAsync(Permissions.CAMERA_ROLL);
  if (permission.status !== 'granted') {
      const newPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      if (newPermission.status === 'granted') {
        //its granted.
      }
  } else {
   ....your code
  }
}

Link to Expo

I edited with some new code. You could just use askAsync entirely, you decide. The docs are very helpful!

like image 50
Sebastian Berglönn Avatar answered Nov 08 '22 13:11

Sebastian Berglönn