joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 772 forks source link

fs.exists on "content://" uri resolves to false #614

Closed NanduKkd closed 2 years ago

NanduKkd commented 4 years ago

I use react-native-receive-sharing-intent to read the files which have been shared to my app. Sometimes it returns only content:// uri. When I shared from Whatsapp, my app received uri content://com.whatsapp.provider.media/item/238353. When I call the function RNFetchBlob.fs.exists("content://com.whatsapp.provider.media/item/238353"), it resolves into false. But when I shared a photo from the default photo gallery app of my android phone, it returns uri content://media/external/images/media/233924 (in this case react-native-receive-sharing-intent does return a file path), which when checked by fs.exists function resolves into true. But in both cases, @react-native-firebase/storage ref('abcd.jpg').putFile(uri) works and succesfully uploads the selected file.

react-native: 0.62.2 rn-fetch-blob: 0.12.0

NanduKkd commented 4 years ago

I researched a bit and joined the bits and pieces and successfully made it to work.

async function copy_uri(uri){
    const blob = await (await fetch(uri)).blob()
    await new Promise(resolve => {
        var reader = new FileReader();
        reader.readAsDataURL(blob); 
        reader.onloadend = () => {
            var base64data = reader.result;                
            let pth = FetchBlob.fs.dirs.SDCardDir+'/mydir/copied.jpg'
            RNFetchBlob.fs.writeFile(pth, reader.result.substr(base64data.indexOf(',')+1), 'base64').then(() => {
                blob.close()
                resolve();
            });
        }
    })
}

I don't know if this is the correct method. If anyone here knows better, please help!