最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Missing camera roll permission in expo - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 17

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!

发布评论

评论列表(0)

  1. 暂无评论