我尝试将本地镜像从手机保存到 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/