joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.84k stars 787 forks source link

Android Downloads Suddenly Crashes App After Working Fine #269

Open Rob2k9 opened 5 years ago

Rob2k9 commented 5 years ago

ok so my app was working perfect downloading with no issues at all i tried it on a firestick / nvidia shield and android TV emulator and it was working perfect one day but the next day when i opened the app to try it again the app crashes every time a download starts.

this is the version of react-native i am using and the version of rn-fetch-blob

"react-native": "0.57.8", "rn-fetch-blob": "^0.10.15"

i have tried creating a new project and adding in just the modules needed for the downloads and used a static link to an apk file i know works perfectly fine and it does work perfect for a few tries but again when the app is closed and reopened sometime later it crashes again after a download starts.

this is the code for my downloads

console.log("Download Starting");

    const android = RNFetchBlob.android;
    const filename = APK_TITLE + ((Math.random() * 1000) | 0) + '.apk';
    const filePath = RNFetchBlob.fs.dirs.DownloadDir + '/' + filename ;

    console.log('Saving File To : ' + filePath);

    this.setState({AppName: APK_TITLE});
    this.setState({ Mode: 'download' });

    let APKDownload = RNFetchBlob.config({

            addAndroidDownloads : {
                useDownloadManager : false,
                mediaScannable : true,
                notification : true,
                mime : 'application/vnd.android.package-archive',
                title : filename,
                description : APK_DESC,
                path: filePath
            }
        })
        .fetch('GET', APK_URL);

    jobId = APKDownload.taskId;
    console.log(jobId);

    setTimeout(()=>{

        APKDownload.progress({ count : 1 },(received,total)=>{
            const percentage = (received / total).toFixed(2);
            const text = percentage;
            console.log('Download Progress : ' + text);
            this.setState({progress: text});
        })
    }, 0)

    APKDownload.then(async (res)=>{

            this.setState({Mode: 'apks'});
            this.setState({AppName: ''});
            this.setState({progress: 0.00});

            console.log('Installing APK');
            android.actionViewIntent(res.path(), 'application/vnd.android.package-archive');

    }).catch((err)=>{
       //you must add this line. If you don't add, your app users waste they're storage
        //RNFetchBlob.fs.unlink(filePath);
        console.log(err)
    })

}

after attaching a debugger in android studio i have found this

I/ReactNativeJS: Download Process Starting
    APK Saving To : /storage/emulated/0/Download/Cyberflixx150.apk
I/ReactNativeJS: Download ID Assigned : wz3qit80wzj2h01bn6n5
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.update, PID: 14132
    java.lang.IllegalArgumentException: path can't be null
        at android.app.DownloadManager.validateArgumentIsNonEmpty(DownloadManager.java:1418)
        at android.app.DownloadManager.addCompletedDownload(DownloadManager.java:1375)
        at android.app.DownloadManager.addCompletedDownload(DownloadManager.java:1325)
        at com.RNFetchBlob.RNFetchBlobReq$3.onResponse(RNFetchBlobReq.java:443)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)

the path is clearly set and even if i remove the let apk path veriable and set the path in path: still the same error :/

bozhouyongqi commented 5 years ago

same issue, did you solved it later?

bozhouyongqi commented 5 years ago

I have solved this problem,but i used the downloadManager.Following is the code:

        const android = RNFetchBlob.android;
        const filename = 'name.apk';
        const filepath = RNFetchBlob.fs.dirs.DownloadDir + '/' + filename;
        const downloadAppUrl = ''http://www.moji.com/android_down.php';';// moji weather apk

        RNFetchBlob.config({
            addAndroidDownloads: {
                useDownloadManager: true,
                title: 'great, download success',
                description:'an apk that will be download',
                mime: 'application/vnd.android.package-archive',
                // mime: 'image/jpeg',
                // mediaScannable: true,
                notification: true,
                path: filepath
            }
        })
        .fetch('GET', downloadAppUrl)
        .then((res) => {
            // console.log('res.path ', res.path());
            alert('res.path ', res.path());
            android.actionViewIntent(res.path(), 'application/vnd.android.package-archive');
        })
        .catch((err) => {
            alert('download error, err is', JSON.stringify(err));
        });

The difference with the official document is that i added the path optional field in the addAndroidDownloads object.When i delete the path field,the it will come into the catch code,then download the app will be wrongly.

the version of react-native i am using and the version of rn-fetch-blob are "react-native": "0.57.3", "rn-fetch-blob": "^0.10.15", and the os is macOS 10.13.6

Rob2k9 commented 5 years ago

Hi yes i solved this issue and got is working along with react-native progress circle my only issue i have now is i cant seem to cancel downloads once they have started

bozhouyongqi commented 5 years ago

Hi yes i solved this issue and got is working along with react-native progress circle my only issue i have now is i cant seem to cancel downloads once they have started

I have not use the cancel download function,but as the official document says,we could use the Cancel Request after rn-fetch-blob 0.7.0.So may be you could try it. https://github.com/joltup/rn-fetch-blob#cancel-request

mahdi-gh33 commented 5 years ago

@Rob2k9 Could you share your code that resolve this problem ? I need a progress in my app to show user.