yazmnh87
yazmnh87

Reputation: 326

Use CameraRoll.saveToCameraRoll to save image to an album?

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'));
}

};

Upvotes: 3

Views: 5339

Answers (1)

Hend El-Sahli
Hend El-Sahli

Reputation: 6752

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);
    });
};

Upvotes: 1

Related Questions