ammarahm-ed / react-native-scoped-storage

MIT License
58 stars 10 forks source link

How to read file .. #26

Open mauro-rogledi opened 2 years ago

mauro-rogledi commented 2 years ago

This is my example code

async function WriteScoped() {
  let dir = await ScopedStorage.openDocumentTree(true);
  console.log('DIR', dir);
  let text = 'Hello world';
  if (dir) {
    var result = await ScopedStorage.writeFile(
      dir.uri,
      'helloworld.txt',
      'text/plain',
      text,
      'utf8',
    );
    console.log('SCOPED WRITED', result);
  }
}

async function ReadScoped() {
  let dir = await ScopedStorage.openDocumentTree(true);
  console.log('DIR read', dir);
  if (dir) {
    let result = await ScopedStorage.readFile(
      dir.uri + '%2Fhelloworld.txt',
      'utf8',
    );
    console.log('SCOPED READED');
  }
}

When use ReadScoped the result is:

Possible Unhandled Promise Rejection
'content://com.android.externalstorage.documents/tree/primary%3ADocuments%2Fhelloworld.txt'is not a file

also If I use

dir.uri + '/helloworld.txt',
reendev commented 2 years ago

Use this:

await ScopedStorage.openDocument(true,'base64').then((content) => { });

This will let the user to choose the file