react-native-scoped-storage icon indicating copy to clipboard operation
react-native-scoped-storage copied to clipboard

Files is saved empty to the download folder

Open AlmiranteTim opened this issue 3 years ago • 7 comments

Hello. On some phones (or Android systems), files are saved with a weight of 0 bytes. In other directories everything is saved normally.

NetworkRepository.DownloadService.downloadAppealFileAndroid(this.item?.code, item).then(async (response) =>  {
    let base64Str = response.data;
    let fileName = item.name;
    let data = base64Str;
    let encoding = "base64";
    try{
      await ScopedStorage.openDocumentTree(true).then(async (file) => {
        await ScopedStorage.writeFile(file.uri, fileName, '*/*',  data, encoding, false).then(() => {
          this.isAppealFileLoad = false;
        })
      });
    } catch (e) {
      this.isAppealFileLoad = false;
      console.log(`***error*** ${e}`)
    }
  });

AlmiranteTim avatar Jan 18 '22 09:01 AlmiranteTim

@AlmiranteTim You are not allowed to save files in Downloads folder or even take permission of that directory in Android 11 and 12. Hence the issue. It works normally on older android versions.

To solve this, don't take permission of Downloads folder root as it won't work on Android 11 and Above. If you really want to save something in the Downloads folder, use createDocument method which allows saving a file there.

ammarahm-ed avatar Jan 18 '22 10:01 ammarahm-ed

@AlmiranteTim You are not allowed to save files in Downloads folder or even take permission of that directory in Android 11 and 12. Hence the issue. It works normally on older android versions.

To solve this, don't take permission of Downloads folder root as it won't work on Android 11 and Above. If you really want to save something in the Downloads folder, use createDocument method which allows saving a file there.

I tried, but the pdf file still saves with a size of 0 bytes. Android - 7.1.2, phone - meizu M6 Note

NetworkRepository.LkService.loadElnFile(lnCode).then(async (response) =>  {
  let base64Str = response.data;
  let fileName = `ELN_${lnCode}.pdf`;
  let mime = 'pdf';
  let data = base64Str;
  let encoding = "base64";
  try{
    if (Platform.OS === "android") {
      return await ScopedStorage.createDocument(fileName, '*/*', data, encoding).then(() => {
        this.isElnFileLoad = false;
      })

AlmiranteTim avatar Jan 18 '22 11:01 AlmiranteTim

@AlmiranteTim The library is being used in a project with thousands of users and I haven't had such a report or issue. I will try to reproduce in emulator & older android versions. Have you tried to reproduce it in a emulator with android 7 installed?

ammarahm-ed avatar Jan 27 '22 04:01 ammarahm-ed

@AlmiranteTim The library is being used in a project with thousands of users and I haven't had such a report or issue. I will try to reproduce in emulator & older android versions. Have you tried to reproduce it in a emulator with android 7 installed?

Yes, i tried. We also received a couple of messages from users about this problem on android 28,29

AlmiranteTim avatar Jan 27 '22 09:01 AlmiranteTim

Does it cause an issue in the emulator?

ammarahm-ed avatar Jan 27 '22 11:01 ammarahm-ed

Does it cause an issue in the emulator?

No

AlmiranteTim avatar Jan 27 '22 11:01 AlmiranteTim

I have the same problem as this issue

aminhdev avatar May 24 '23 10:05 aminhdev