ammarahm-ed / react-native-scoped-storage

MIT License
58 stars 10 forks source link

"Error: does not have permission to read/write" #27

Open SohelKabir opened 2 years ago

SohelKabir commented 2 years ago

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. (D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:128) at step (D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:32) at Object.next (D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:13) at D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:7 at tryCallTwo (D:\Nanoid\halkhata-app\node_modules\react-native\node_modules\promise\setimmediate\core.js:45) at doResolve (D:\Nanoid\halkhata-app\node_modules\react-native\node_modules\promise\setimmediate\core.js:200) at new Promise (D:\Nanoid\halkhata-app\node_modules\react-native\node_modules\promise\setimmediate\core.js:66) at __awaiter (D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:3) at Object.readFile (D:\Nanoid\halkhata-app\node_modules\react-native-scoped-storage\dist\index.js:125)

console.log('==============error======================');

SohelKabir commented 2 years ago

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.