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

jam*_*phy 2 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()
Run Code Online (Sandbox Code Playgroud)

请参阅下面的代码。 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);
  }
};
Run Code Online (Sandbox Code Playgroud)

清单如下:

<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"/> 
Run Code Online (Sandbox Code Playgroud)

Muh*_*man 5

解决方案一:

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
}
Run Code Online (Sandbox Code Playgroud)
const fileUri = await getPathForFirebaseStorage(file.uri)
  const uploadTask = ref.putFile(fileUri)
Run Code Online (Sandbox Code Playgroud)

解决方案2:

import RNFS from 'react-native-fs'

const data = await RNFS.readFile(uri, 'base64')
await ref.putString(data, 'base64')
Run Code Online (Sandbox Code Playgroud)

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