如何获取React Native中从Firebase存储下载的图像文件的本地文件路径

时间:2020-07-04 07:23:04

标签: firebase react-native firebase-storage

在我的React Native应用中,我需要

  1. 将图像上传到Firebase存储
  2. 然后下载并编辑(裁剪/旋转)
  3. 再次将已编辑的图像上传到Firebase存储

我能够上载图像,使用getDownloadURL()方法获取其URL,并使用DEBUG:__main__:b'select count(*) from some_table where id > 1000' 组件显示该图像,因为它会立即获取URL输入。

现在,我需要本地文件路径,以便将已编辑的图像编辑并重新上传到Firebase Storage。

How do I use Psycopg2's LoggingConnection?介绍了一种Image方法,但我找不到This question。 另一个firebase docs给出了一些指示,但在JAVA中却给出了指示。

question提到使用诸如getFile()react-native-fs之类的库,但是看来他们正在尝试将文件保存在存储/画廊中。这对于我的要求是不必要的(我不想将图像保存在图库中)。我可能只需要下载图像的缓存图像的路径。

有人可以解释一下如何在React Native语言中获取下载的图像文件的本地文件路径吗?

编辑

不使用Expo。而且我希望它可以同时与Android和iOS一起使用。

2 个答案:

答案 0 :(得分:0)

npm install rn-fetch-blob

请按照以下步骤操作:

a)按照安装说明进行操作。

a2)如果要在不使用rnpm的情况下手动安装软件包,请转到其wiki

b)最后,这就是我能够在应用程序中下载文件的方式:

const { config, fs } = RNFetchBlob
let PictureDir = fs.dirs.PictureDir // this is the pictures directory. You can check the available directories in the wiki.
let options = {
  fileCache: true,
  addAndroidDownloads : {
    useDownloadManager : true, // setting it to true will use the device's native download manager and will be shown in the notification bar.
    notification : false,
    path:  PictureDir + "/me_"+Math.floor(date.getTime() + date.getSeconds() / 2), // this is the path where your downloaded file will live in
    description : 'Downloading image.'
  }
}
config(options).fetch('GET', "http://www.example.com/example.pdf").then((res) => {
  // do some magic here
})

OR

const { uri: localUri } = await FileSystem.downloadAsync(remoteUri, FileSystem.documentDirectory + 'name.ext');

答案 1 :(得分:0)

安装react-native-fs

yarn add react-native-fs

并可以获得这样的临时路径

var RNFS = require('react-native-fs');

const imageUrl = `http://www.example.com/abc.png`;
const imagePath = `${Platform.OS==="android"?"/storage/emulated/0/Download":RNFS.TemporaryDirectoryPath}/${((Math.random() * 1000) | 0)}.jpeg`;
RNFS.downloadFile({
        fromUrl: imageUrl,
        toFile: imagePath
    }).promise
        .then((result) => {
          console.log(imagePath);  //here you get temporary path
    })
      .catch((e) => {
        console.log(e,"error");
      })