react-native - React Native Firebase Storage - 尝试使用 .putFile() 保存图像时出现错误

标签 react-native firebase-storage react-native-firebase

我尝试将本地镜像从手机保存到 Firebase 存储,但收到以下错误。

Permission Denial: reading com.google.android.apps.photos.contentprovider/........uid=XXXXX requires the provider be exported, or grantUriPermission()

参见下面的代码。 await imageRef.putFile(localUri, {contentType: 'image/jpg'}); 是问题发生的地方

const uploadImageAndGetUrl = async (localUri, firebasePath) => {
  try {
    const imageRef = storage().ref(firebasePath);
    await imageRef.putFile(localUri, {contentType: 'image/jpg'});
    const url = await imageRef.getDownloadURL();
    return url;
  } catch (err) {
    Alert.alert('Profile.js.....Error getting image url from FB', err);
  }
};

list 如下:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

最佳答案

解决方案1:

import RNFetchBlob from 'rn-fetch-blob'

async function getPathForFirebaseStorage (uri) {
  if (Platform.OS==="ios") return uri
  const stat = await RNFetchBlob.fs.stat(uri)
  return stat.path
}
const fileUri = await getPathForFirebaseStorage(file.uri)
  const uploadTask = ref.putFile(fileUri)

解决方案 2:

import RNFS from 'react-native-fs'

const data = await RNFS.readFile(uri, 'base64')
await ref.putString(data, 'base64')

注意:如果上述解决方案不起作用,请确保使用react-native-permissions库的用户具有READ_EXTERNAL_STORAGE权限

关于react-native - React Native Firebase Storage - 尝试使用 .putFile() 保存图像时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59915763/

相关文章:

firebase - 在 React Native 一段时间后删除通知(@react-native-firebase/messaging)

javascript - NavigatorIOS 转换导致图像闪烁

ios - 即使在终止并重新启动应用程序后,如何在 native iOS 中下载音频文件?

java - 无法将 getDownloadUrl() 的响应保存到变量

android - 将多张图片上传到 firebase 存储

javascript - 如何将多个文件上传到 Firebase?

react-native - 收到仅数据的Firebase云消息后,将Android React Native App带到前台

android - 构建生产时出错 react native firebase 应用程序

android - 如何删除React Native上的所有应用程序数据?

node.js - Macbook React Native 安装