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 });
}
};
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!
Share Improve this question asked Aug 22, 2018 at 11:28 crodevcrodev 1,4916 gold badges25 silver badges45 bronze badges1 Answer
Reset to default 17If 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!