ammarahm-ed / react-native-scoped-storage

MIT License
58 stars 10 forks source link

Files is saved empty to the download folder #24

Open AlmiranteTim opened 2 years ago

AlmiranteTim commented 2 years ago

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}`)
    }
  });
ammarahm-ed commented 2 years ago

@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.

AlmiranteTim commented 2 years ago

@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;
      })
ammarahm-ed commented 2 years ago

@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?

AlmiranteTim commented 2 years ago

@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

ammarahm-ed commented 2 years ago

Does it cause an issue in the emulator?

AlmiranteTim commented 2 years ago

Does it cause an issue in the emulator?

No

aminhdev commented 1 year ago

I have the same problem as this issue