3

I'm using RNCamera and its working great(Below is my function). I can take images and save them to the photo gallery. Although anyone have insight on how to save to a specific album on IOS? I've seen solutions for Android, there are no options for the method either. Not sure how to solve this. TIA. I'm thinking I may need to edit the uri? Doesn't seem right but just searching for ideas. Possibly I would need to modify the library file 'CameraRoll.js' in node_modules/react_native/Libraries/CameraRoll?

-Read react-native docs, https://facebook.github.io/react-native/docs/cameraroll.html

-Searched stackoverflow(found thread with android solution)/google

async takePicture() {
if (this.camera) {
  const options = { quality: 0.5, based64: true }
  const data = await 
this.camera.takePictureAsync(options).then(data => 
    console.log(data.uri))
    CameraRoll.saveToCameraRoll(data.uri, 'photo'));
}

};

yazmnh87
  • 316
  • 5
  • 12
  • **UPDATE** I ended up using https://www.npmjs.com/package/react-native-store-photos-album and the CameraRollExtended component. This allowed me to easily save the pictures to the album of my choice. – yazmnh87 Jun 30 '19 at 20:18

1 Answers1

1

You need to npm install react-native-fs ... and here's your saveFile method:

const saveFile = (base64) => {
  const albumPath = `${RNFS.PicturesDirectoryPath}/${APP_NAME}`;

  const fileName = `${new Date().getTime()}.png`;
  const filePathInCache = `${RNFS.CachesDirectoryPath}/${fileName}`;
  const filePathInAlbum = `${albumPath}/${fileName}`;

  return RNFS.mkdir(albumPath)
    .then(() => {
      RNFS.writeFile(filePathInCache, base64, 'base64').then(() => RNFS.copyFile(filePathInCache, filePathInAlbum)
      // Next step to show album without the need to re-boot your device:
        .then(() => RNFS.scanFile(filePathInAlbum))
        .then(() => {
          console.log('File Saved Successfully!');
        }));
    })
    .catch((error) => {
      console.log('Could not create dir', error);
    });
};
Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42