2

I am using react native cameraroll api to save my images in storage.

CameraRoll.saveToCameraRoll(data.uri)
            .then(console.log('Success', 'Photo added to camera roll!'))
            .catch(err => console.log('err:', err))

Above code successfully save image in storage like (DCIM/imageName.jpg)

But i want to change the location of images to be saved somewhere else like (testfolder/1/imagename.jpg) I tried like this:

CameraRoll.saveToCameraRoll({
            uri: data.uri,
            album: 'test',
        }, 'photo').then((newUri) => {
            console.log('new location of image => ', newUri);
        })

I read this link https://facebook.github.io/react-native/docs/cameraroll.html#savetocameraroll but there is no proper guide or parameter set to change location.

Anyone guide me how can i change path for saving images?

4

2 回答 2

2

正如我在这里所说:如何将图片从 React Native Camera Roll 保存到特定文件夹

在 Android 上,您可以使用 react-native-fetch-blob 及其文件系统访问 API(PictureDir 常量)。

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

于 2018-04-29T13:56:20.133 回答
0

我知道我回答这个问题已经太晚了。但是我在没有使用 rn-fetch-blob 库的情况下得到了解决方案。

你可以简单地使用下面的 npm 包来获得你想要的输出

npm i @react-native-community/cameraroll

之后,您必须在 try catch 块中使用以下代码,以便您可以轻松地将图像存储在所需的路径上。

   try{
        CameraRoll.save(data.uri,{type:"photo",album:"../your folder name"}).
                    then((res)=>{console.log("save img...",res);}).
                    catch((err)=>{console.log("err for save img...",err);})
    }
    catch{
      //your code...
    }
于 2021-05-14T11:40:08.997 回答