javascript - Unable to get base64 data from ImagePicker in Expo - Stack Overflow

I am currently trying to develop a simple application using Expo and react-native and have run into a p

I am currently trying to develop a simple application using Expo and react-native and have run into a problem that i cannot overe. I need to aquire pictures in the base64 format both using camera and from gallery. For this purpose I decided to use the ImagePicker expo ponent. When I access pictures form gallery using

const responsey = await ImagePicker.launchImageLibraryAsync({base64: true, quality: 1,});

It works like a charm, but when I try to access camera feed via

const response = await ImagePicker.launchCameraAsync({base64: true, quality: 1,});

The promise never resolves and the application is stuck waiting for it. I read in documentation about the ImagePicker.getPendingResultAsync() method and tried using is as such:

  const prom1 =  ImagePicker.launchCameraAsync({base64: true, quality: 1,});
  const prom2 = ImagePicker.getPendingResultAsync();
  response = await any([prom1,prom2]);

But the result of this is an empty array (I imagin returned instantly form the second promise) Where any is the function from promise.any package.

Additionaly I have noticed that when I don't request the base64 format the promise from launchCameraAsync resolves just fine. I am reunning the appliaction on Android 10.

I am struggling to resolve this problem and been stuck on int for several days, I would be gratefull for any advice or direction as to how to solve this.

I am currently trying to develop a simple application using Expo and react-native and have run into a problem that i cannot overe. I need to aquire pictures in the base64 format both using camera and from gallery. For this purpose I decided to use the ImagePicker expo ponent. When I access pictures form gallery using

const responsey = await ImagePicker.launchImageLibraryAsync({base64: true, quality: 1,});

It works like a charm, but when I try to access camera feed via

const response = await ImagePicker.launchCameraAsync({base64: true, quality: 1,});

The promise never resolves and the application is stuck waiting for it. I read in documentation about the ImagePicker.getPendingResultAsync() method and tried using is as such:

  const prom1 =  ImagePicker.launchCameraAsync({base64: true, quality: 1,});
  const prom2 = ImagePicker.getPendingResultAsync();
  response = await any([prom1,prom2]);

But the result of this is an empty array (I imagin returned instantly form the second promise) Where any is the function from promise.any package.

Additionaly I have noticed that when I don't request the base64 format the promise from launchCameraAsync resolves just fine. I am reunning the appliaction on Android 10.

I am struggling to resolve this problem and been stuck on int for several days, I would be gratefull for any advice or direction as to how to solve this.

Share Improve this question asked May 12, 2021 at 19:18 MarkusSPEMarkusSPE 911 silver badge11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Arrange your code like that to get the base 64 value.

const openCamera = async () => {
    // Ask the user for the permission to access the camera
    const permissionResult = await ImagePicker.requestCameraPermissionsAsync();

    if (permissionResult.granted === false) {
      alert("You've refused to allow this appp to access your camera!");
      return;
    }

    const result = await ImagePicker.launchCameraAsync({
      base64: true,
      quality: 1,
    });

    // Explore the result
    console.log(result.base64);

    if (!result.cancelled) {
      
    }
};

Get the image's file uri which looks like this - file://.....

Get this uri and change the file to base64 format using ImageManipulator from Expo.

don't forget to add base64: true

let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
base64: true, 

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745203868a4616489.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信