react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

Can't read file from downloads with uri on Android

Open kilaniamine96 opened this issue 4 years ago • 6 comments

Hello,

RNFs version used : 2.16.6

I am trying to read the size of a document with RNFS.stat.

 DocumentPicker.pick({
            type: [DocumentPicker.types.pdf],
        }).then((res) => {
            RNFS.stat(res.uri).then((result) => {
                console.log(${result.size});
            }).catch((e) => {
                console.log("Read dir error", e);
            });
            console.log(
                \nuri : ${res.uri} \n
                type : ${res.type} \n
                name : ${res.name} \n
                size : ${res.size} octets \n
                
            );

It's working well on iOS and the value of res.uri is like this: "file:///private/var/mobile/Containers/Data/Application/..."

On Android I get an "Error: File does not exist". The uri in Android looks like : "content://com.android.providers.downloads.documents/document/3627". It seems like RNFS can't use Android Uri to read a file. The thing is I can only get this uri since it's coming from another library react-native-document-picker. The DocumentPicker does not provide a filepath.

Do you know a way to read, save or write a document when we only have its uri on Android, without having to write native java code ?

Thank you

kilaniamine96 avatar May 12 '20 09:05 kilaniamine96

Hello friend, have you found a solution to this? Currently running into the same issue. Also using react-native-document-picker

lundjrl avatar May 20 '20 17:05 lundjrl

Hello, Nop didn't found a solution, I searched and even looked at native code trying to understand how to do this. I'm still waiting for the answer of the community then ..

kilaniamine96 avatar May 22 '20 07:05 kilaniamine96

So actually we can't get the uri from android in this way. It does need to be converted to a blob first. Personally, I use react-native-fs to achieve this

lundjrl avatar May 22 '20 11:05 lundjrl

I copy to file inside the temporary folder. Copy from content seems to work.

matteodanelli avatar Jul 01 '20 10:07 matteodanelli

@lundjrl @kilaniamine96 I am facing the same issue, how did you solve this problem ? any other way to get base64 from PDF when using react-native-document-picker

dev3288 avatar Aug 05 '20 13:08 dev3288

rn-fetch-blob works for me, ref to https://github.com/itinance/react-native-fs/issues/756#issuecomment-554937928

flyskywhy avatar Mar 22 '22 06:03 flyskywhy