【问题标题】:React Native save image to specific locationReact Native 将图像保存到特定位置
【发布时间】:2018-03-30 05:48:27
【问题描述】:

我正在使用 react native cameraroll api 将我的图像保存在存储中。

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

以上代码成功将图像保存在存储中,如 (DCIM/imageName.jpg)

但我想更改图像的位置以保存在其他地方,例如 (testfolder/1/imagename.jpg) 我试过这样:

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

我阅读了此链接https://facebook.github.io/react-native/docs/cameraroll.html#savetocameraroll,但没有适当的指南或参数集来更改位置。

谁能指导我如何更改保存图像的路径?

【问题讨论】:

  • 有人有解决办法吗?
  • DIGITAL JEDI,有答案吗?如果是,请分享。

标签: javascript android reactjs react-native camera-roll


【解决方案1】:

正如我在这里所说的:How to save a Picture to a specific folder from 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

【讨论】:

    【解决方案2】:

    我知道我回答这个问题为时已晚。但是我得到了没有使用 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...
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多