Search code examples
react-nativefirebase-storagereact-native-firebase

React Native Firebase Storage - getting error when trying to save image with .putFile()


I am trying to save a local image from the phone to firebase storage but am getting the following error.

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

See below code. await imageRef.putFile(localUri, {contentType: 'image/jpg'}); is where the issue is occuring

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);
  }
};

Manifest is as follows:

<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"/> 

Solution

  • Solution 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)
    

    Solution 2:

    import RNFS from 'react-native-fs'
    
    const data = await RNFS.readFile(uri, 'base64')
    await ref.putString(data, 'base64')
    

    Note: if above solution do not work then make sure READ_EXTERNAL_STORAGE permissions from the user using the react-native-permissions library