How to save a Picture to a specific folder from React Native Camera Roll

老子叫甜甜 提交于 2019-12-22 11:27:35

问题


So I'm using Expo's Camera API to take a picture, like so:

takePicture = async function() {
  if(this.camera) {
    this.camera.takePictureAsync().then(data => {
        CameraRoll.saveToCameraRoll(data.uri);
    }).then(() => {
      this.setState({
        photoId: this.state.photoId + 1,
      });
    });
  }
};

Now, I'd like to specifically retrieve the pictures I take with this app to work with later. I figure an easy way to do this would be if the app saved its pictures to a separate folder in the camera roll, much like how Instagram makes its own folder in there.

How would I go about having the app create its own folder in the camera roll, and then saving pictures to it?


回答1:


On Android, you can use react-native-fetch-blob and its File System Access API (PictureDir constant).

For example, I do:

try {
    const data = await this.camera.takePictureAsync()
    await RNFetchBlob.fs.mkdir(`${RNFetchBlob.fs.dirs.PictureDir}/myfolder`)
    await RNFetchBlob.fs.mv(
        data.api, 
        `${RNFetchBlob.fs.dirs.PictureDir}/myfolder/${moment().unix()}.jpg`
    )
} catch () {
    ...
}

https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API



来源:https://stackoverflow.com/questions/48151242/how-to-save-a-picture-to-a-specific-folder-from-react-native-camera-roll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!