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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With