Rn-fetch-blob: How to read from camera roll?

Created on 31 Oct 2018  路  4Comments  路  Source: joltup/rn-fetch-blob

It looks like this is documented in several areas in the readme, but I still can't read from the iOS camera roll. It's my understanding that I'm supposed to use a platform-specific url path conditioner like the one below, but I keep getting "file not found" errors.

  let path = ''
    console.log('FIRST PATH = ', upload.draft.file.path)
    if (Platform.OS === 'ios') {
      let arr = upload.draft.file.path.split('/')
      const dirs = RNFetchBlob.fs.dirs
      path = `${dirs.DocumentDir}/${arr[arr.length - 1]}`
      console.log('SECOND PATH = ', path)
    } else {
      path = upload.draft.file.path
    }

// outputs:
// FIRST PATH = file:///Users/dandre/Library/Developer/CoreSimulator/Devices/998C742E-3EE7-45B1-8B45-10BC0CB96EF9/data/Media/DCIM/100APPLE/IMG_0005.JPG

// SECOND PATH = /Users/dandre/Library/Developer/CoreSimulator/Devices/998C742E-3EE7-45B1-8B45-10BC0CB96EF9/data/Containers/Data/Application/82677F63-BCE9-4F2F-A2F6-D4DF861FF212/Documents/IMG_0005.JPG


    RNFetchBlob.fs.readFile(path, 'base64')
      .then(base64File => {
        dispatch(uploadPost(upload, location, base64File))
      }).catch(errors => console.warn(errors.message))

Is there an obvious issue here that I'm missing? I'm trying to take an image from the camera roll and upload it to our server as a base64 file.

Most helpful comment

Yeah, I figured that out too. I didn't see it mentioned anywhere in the docs but hopefully this will help someone else trying to read from ios camera roll: path.split('//')[1].

All 4 comments

I found the answer, had to strip out file:// from the URL before upload

Yeah, I figured that out too. I didn't see it mentioned anywhere in the docs but hopefully this will help someone else trying to read from ios camera roll: path.split('//')[1].

Hi. I'm facing the same issue. I'm not getting to the solution. Wouldn't the file:// be already stripped of after the iOS specific code is executed? Like the SECOND PATH, it doesn't have file at the beginning so what is wrong there?

If CameraRoll is providing to you this kind of uri ph://A8CD991C-2306-44FE-A19C-E4B3C24C6E0E/L0/001 then follow this. As you can see what they are doing to form a proper uri is:

const appleId = uri.substring(5, 41);
const ext = isPhoto ? 'JPG' : 'MOV';
return `assets-library://asset/asset.${ext}?id=${appleId}&ext=${ext}`;
Was this page helpful?
0 / 5 - 0 ratings