react-native-scoped-storage
react-native-scoped-storage copied to clipboard
"Error: does not have permission to read/write"
Even after calling
let dir = await ScopedStorage.openDocumentTree(true)
and getting permission to specific directory it doesn't allow to read file but can be write without any problem.
Using same uri which i got from writing if I use that to read it gives me the permission error. Below I have provided my code snippet.
const testFunc = async () => {
try {
let dir = await ScopedStorage.openDocumentTree(true);
const jsonData = await JSON.stringify(state, null, 2);
const fileName = `data-${authInfo.phoneNum}`;
const createDir = await ScopedStorage.createDirectory(
dir.uri,
'.hkbackup',
);
console.log('=======createDir=============================');
console.log(createDir);
console.log('==========createDir==========================');
const wroteFile = await ScopedStorage.writeFile(
createDir.uri,
fileName,
'application/octet-stream', // 'application/json',
jsonData,
'utf-8',
);
console.log('===============wroteFile=====================');
console.log(wroteFile);
console.log('=============wroteFile=======================');
const stat = await ScopedStorage.stat(wroteFile);
console.log('==========stat==========================');
console.log(stat);
console.log('============stat========================');
const readDoc = await ScopedStorage.openDocument(true, 'utf8');
console.log('=======readDoc=============================');
console.log(readDoc);
console.log(readDoc.uri);
console.log('=========readDoc===========================');
const readFile = await ScopedStorage.readFile(readDoc.uri, 'utf8');
console.log('=======readFile=============================');
console.log(readFile);
console.log('=========readFile===========================');
} catch (error) {
console.log('===============error=====================');
console.log(error);
console.log('==============error======================');
}
};
testFunc();
Error given below
```
console.log('===============error=====================');
Error: 'content://com.android.externalstorage.documents/document/primary%3ADocuments%2F.hkbackup%2F.hkbackup%2Fdata-01758515157'does not have permission to read/write
at Object.promiseMethodWrapper [as readFile] (D:\Nanoid\halkhata-app\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:103)
at Object.
console.log('==============error======================');
I got it working! this is pretty weird you have to provide exact uri which you got during write. I was trying to provide uri which I received from ScopedStorage.openDocument call.