Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expo FileSystem.readAsStringAsync could not read file

I have the following setup, when user take picture using ImagePicker it will save it to FileSystem.documentDirectory using the following piece of code:

saveAvatar = async (uri) => {
   await Expo.FileSystem.moveAsync({
   from: uri,
   to: Expo.FileSystem.documentDirectory + 'avatar/profile'
  })
}

_takePhoto = async () => {
  const result = await ImagePicker.launchCameraAsync({
    allowsEditing: false,
    base64: true,
    quality: 0.4
  });

  if (!result.cancelled) {
    this.setState({ image: result.base64 });
    this.saveAvatar(result.uri)
  }
}; 

Then I tried checking retrieving it using this:

ensureDirAsync = async () => {
    const props = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'avatar/');

    if (props.exists && props.isDirectory) {
        return props;
    }

    try {
        await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'avatar/', { intermediates: true });
    }
    catch (e) {
        console.log(e);
    }

    return await this.ensureDirAsync()
}

getAvatar = async () => {
    let dir = await this.ensureDirAsync(),
        filename = await FileSystem.readDirectoryAsync(dir.uri),
        data = null;
    const props = await FileSystem.getInfoAsync(dir.uri + filename[0])
    console.log(props)
    try {
        data = await FileSystem.readAsStringAsync(FileSystem.documentDirectory + 'avatar/profile');
    }
    catch (e) {
        console.log(e);
    }
    console.log(data)
    return data;
}

The weird thing is, const props = await FileSystem.getInfoAsync(dir.uri + filename[0]) is printing this:

Object {
 "exists": 1,
 "isDirectory": false,
 "modificationTime": 1532930978,
 "size": 399861,
 "uri": "file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076-b4b71df1e55c/avatar/profile",
} 

Which should indicate that the image was saved successfully, but data = await FileSystem.readAsStringAsync(dir.uri + filename[0]); OR data = await FileSystem.readAsStringAsync(FileSystem.documentDirectory + 'avatar/profile') would give me this error:

File 'file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076-b4b71df1e55c/avatar/profile' could not be read.

Any idea how could this happen? can FileSystem.readAsStringAsync() even read the file I moved from ImagePicker? if not, what should I have used instead?

I'm trying this on IOS.

Thanks in advance for the help :smiley:

like image 919
Ray Jonathan Avatar asked Jan 27 '23 16:01

Ray Jonathan


1 Answers

You have to specify the EncodingType. FileSystem.EncodingType.Base64 works for audio/image files.

example:

content = await FileSystem.readAsStringAsync(uri, { encoding: FileSystem.EncodingType.Base64 });
like image 195
mr22 Avatar answered Jan 31 '23 18:01

mr22