Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get base64 data from ImagePicker in Expo

I am currently trying to develop a simple application using Expo and react-native and have run into a problem that i cannot overcome. 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 component. 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.

like image 234
MarkusSPE Avatar asked Dec 19 '25 10:12

MarkusSPE


1 Answers

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) {
      
    }
};
like image 125
Muhammad Saeed Avatar answered Dec 21 '25 00:12

Muhammad Saeed