react-native-fs
react-native-fs copied to clipboard
Can't read file from downloads with uri on Android
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
Hello friend, have you found a solution to this? Currently running into the same issue. Also using react-native-document-picker
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 ..
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
I copy to file inside the temporary folder. Copy from content seems to work.
@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
rn-fetch-blob
works for me, ref to https://github.com/itinance/react-native-fs/issues/756#issuecomment-554937928