如何使用 expo 和 react-native 将照片从 firebase 存储保存到 phone 图库?

How do I save photo from firebase storage to phone Gallery with expo and react-native?

我有一张照片 url,我想将照片下载到 phone 存储。我将照片存储在 firebase 上,所以 url 类似于 https://firebasestorage.googleapis.com/v0/b/.../..***0e896313c

据我所知 expo doesnt work with rn-fetc-blob so I started looking for alternative solutions. I thought I should use expo's MediaLibrary 但当我尝试使用 await MediaLibrary.createAssetAsync(uri); 创建资产时,它需要具有扩展名的本地 url。

我是不是遗漏了什么,还是应该使用其他库?

您可以使用expo-file-system下载照片。下载文件后,它会为您提供本地 uri。然后,使用 MediaLibrary 将文件保存到您的图库

// Function to download the photo
const Download = async (uri) => {
  const downloadInstance = FileSystem.createDownloadResumable(
    uri,
    FileSystem.documentDirectory + "image.jpg"
  );

  const result = await FileSystem.downloadInstance.downloadAsync();
}

您的文件将被下载到一个目录中,该目录的 uri 将在 result 对象中。 现在使用 MediabLibrary 将照片保存到图库。

如果您只登录 result.uri,您将获得下载图像的本地 uri。现在

const asset = await MediaLibrary.createAssetAsync(result.uri);

MediaLibrary.createAlbumAsync("MyFolder", asset, false)
   .then(() => console.log('File Saved Successfully'))
   .catch(() => console.log('Error in saving file'));

通过使用上述方法,您的图像将保存在图库中名为“MyFolder”的文件夹中