Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android crashing on download on first time (java.lang.ArithmeticException: divide by zero) #164

Closed
Saad-Bashar opened this issue Jun 21, 2022 · 5 comments

Comments

@Saad-Bashar
Copy link

Saad-Bashar commented Jun 21, 2022

Having this strange issue on android that it is crashing when I try to download my remote PDF file first time. It downloads the file but then the app crashes. It only happens on the first run but on subsequent call it does not crash anymore. Please refer to the screenshot for the crash stack. I have been using BrowserStack devices.

Screenshot 2022-06-21 at 11 46 12 AM

This is my download file,

 const downloadFile = () => {
    let dirs = ReactNativeBlobUtil.fs.dirs;
    ReactNativeBlobUtil.config({
      fileCache: true,
      appendExt: 'pdf',
      path: `${dirs.DocumentDir}/${fileName}`,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        title: fileName,
        description: 'File downloaded by download manager.',
        mime: 'application/pdf',
      },
    })
      .fetch('GET', fileUrl)
      .then((res) => {
        // the temp file path
        console.log('The file saved to ', res.path());
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })
      .catch((err) => console.log('BLOB ERROR -> ', err));
  };

"react-native": "0.66.4",
"react-native-blob-util": "^0.16.0",

@Saad-Bashar Saad-Bashar changed the title Android crashing on download on first time ( java.lang.ArithmeticException: divide by zero, com.ReactNativeBlobUtil.i$a.handleMessage(ReactNativeBlobUtilReq.java:14) Android crashing on download on first time (java.lang.ArithmeticException: divide by zero) Jun 21, 2022
@Saad-Bashar
Copy link
Author

Saad-Bashar commented Jun 22, 2022

Update - After digging a lot, it seems like on android the download is getting called twice somehow on the first run? That might be reason for the crash. As for the other times, I do not get logs 2 times and it does not crash as well. Is it a download manager issue? Is there a way to avoid this multiple calls?

@RonRadtke
Copy link
Owner

What size does the file you're downloading has?
For me it looks like the expected file size is 0 and thus we get an error here.

You could try to add a check here:

float progress = (total != -1) ? written / total : 0;

E.g. by changing it to
float progress = (total > 0) ? written / total : 0;

Please give me a quick hint if it helps or not. So we can dig further or create a fix

@Saad-Bashar
Copy link
Author

Thanks for your kind response! The file size was not 0 for sure, it had few KB. I will add that check and give it a go today and will update you!

@Saad-Bashar
Copy link
Author

@RonRadtke , I believe adding that check fixed the crash! I could not reproduce the same crash anymore! Would you be kind enough to release an update soon with this small fix? Thanks a lot!

@RonRadtke
Copy link
Owner

Yes I will.
I was planning on an update thisnweek or beginning of next week regardless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants