'React Native reading file not allowed after persisting uri in storage
I have an app where a user picks a file with products and the app parses it (reads it) and stores the data in storage. The picked file url is also stored in storage.
The problem is:
When the user himself picks the file, RNFS.readFile reads it without any problem and the file uri i stored in storage.
The next time the user opens the app and loads the file uri from storage, RNFS.readFile says it has no permissions even though all of the required permissions are listed in Manifest file.
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.ACTION_OPEN_DOCUMENT" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
The error i am getting when reading the file with the stored uri is
Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/test.txt from pid=22663, uid=10471 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
I also put requestLegacyExternalStorage in the manifest.
<application
...
android:requestLegacyExternalStorage="true"
>
This is how i pick the file and store it
DocumentPicker.pickSingle({
type: 'text/plain'
}).then(res => {
dispatch(setFileSource(res))
})
.catch(err => console.log(err))
And then i use the stored uri to read the file.
RNFS.readFile(fileSource.uri,'utf8').then(res => ...);
This works only upon selecting the file, but when i close the app and read the file from the stored uri. It gives the error above. I am afraid that persisted uri in storage, when retrieved and used to read the file, is wrong and i need to parse it somehow. Correct me if im wrong and any hints are welcomed!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
