'react native access content:// uri fails
Working with the following react native packages in android:
"react-native": "0.47.2"
"react-native-document-picker": "2.0.0" (removing the ovveride to compile with 0.47)
"react-native-fetch-blob": "^0.10.8"
I'm getting a content:// uri while using react-native-document-picker or react-native-contacts (for the avatars). URIs like:
'content://com.android.providers.media.documents/document/image%3A99'
Trying to access this file fails with "stat error: failed to list path null for it is not exist or it is not a folder'"
const urlDecoded = decodeURIComponent(url);
return new Promise((resolve, reject) => {
RNFetchBlob.fs.stat(urlDecoded)
.then(resolve)
.catch(reject);
});
Any idea how to access the file? access the real path of it?
Solution 1:[1]
There is now a solution for this problem in react-native-fs. Enabling access and copy content:// files -https://github.com/itinance/react-native-fs/pull/395
Solution 2:[2]
check your AndroidManifest.xml and write READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permission and double-check that those permissions are granted.
Solution 3:[3]
I have the same issue with accessing media(content uri) files from an external app like music etc. No need to add anything just follow my answer.
I have fixed it by adding PermissionsAndroid from react-native
here is the code:
"react-native": "0.64.2"
"react-native-document-picker": "^7.1.3",
import {PermissionsAndroid, ToastAndroid} from 'react-native';
const uploadAudio = async finalSubmit => {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission',
message: 'App needs access to memory to upload the file ',
},
);
if (granted != PermissionsAndroid.RESULTS.GRANTED) {
ToastAndroid.showWithGravity(
'You need to give storage permission to upload the file',
ToastAndroid.SHORT,
ToastAndroid.BOTTOM,
);
return false;
}
};
uploadAudio();
I hope this helps you :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | EyalS |
Solution 2 | Yaqoob Bhatti |
Solution 3 | user9088454 |