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 784 forks source link

download manager could not resolve downloaded file path #214

Open Jeijie opened 6 years ago

Jeijie commented 6 years ago

When I downloaded apk by download manager, the error occurred. My code:
const android = RNFetchBlob.android; RNFetchBlob.config({ addAndroidDownloads: { useDownloadManager: true, title: "xxx.apk", description: "downloading", mime: "application/vnd.android.package-archive", mediaScannable: true, notification: true } }) .fetch("GET", appUrl) .then(res => { android.actionViewIntent( res.path(), "application/vnd.android.package-archive" ); });

Environment:

OS: Windows 7 Node: 8.12.0 Yarn: 1.9.4 npm: 6.4.1 Watchman: Not Found Xcode: N/A Android Studio: Not Found

"rn-fetch-blob": "^0.10.12"

Packages: (wanted => installed) react: 16.3.1 => 16.3.1 react-native: ^0.55.4 => 0.55.4

Jeijie commented 6 years ago

after update code ,an error occurred.

Possible Unhandled Promise Rejection(id:0): Error:Attempt to invoke virtual method 'android.content.res.XmlResourceParser......

const android = RNFetchBlob.android; RNFetchBlob.config({ addAndroidDownloads: { useDownloadManager: true, title: "xxx.apk", description: "downloading...", mime: "application/vnd.android.package-archive", mediaScannable: true, path: RNFetchBlob.fs.dirs.DownloadDir + "/" + xxx.apk', notification: true } }) .fetch("GET", appUrl) .then(res => { console.warn('res...',res); android.actionViewIntent( res.path(), "application/vnd.android.package-archive" ); });

mohammad-goldast commented 5 years ago

same issue

thejerrytan commented 5 years ago

same here

vitalyliber commented 5 years ago

The same issue =(

DonghyunNa commented 5 years ago

I use both of RNFS and RNFetchBlob.

I solved this problem with this solution

` var RNFS = require('react-native-fs'); import RNFetchBlob from 'rn-fetch-blob';

let downloadDest = ${RNFS.ExternalStorageDirectoryPath}/Download/123.jpg;

RNFetchBlob.config({ addAndroidDownloads: { useDownloadManager: true, path: downloadDest, notification: true, mime: '/' }).then((res) => { RNFetchBlob.android.actionViewIntent(res.path(), '/') }) `

It works to me. good luck.

mts88 commented 5 years ago

I use both of RNFS and RNFetchBlob.

I solved this problem with this solution

` var RNFS = require('react-native-fs'); import RNFetchBlob from 'rn-fetch-blob';

let downloadDest = ${RNFS.ExternalStorageDirectoryPath}/Download/123.jpg;

RNFetchBlob.config({ addAndroidDownloads: { useDownloadManager: true, path: downloadDest, notification: true, mime: '/' }).then((res) => { RNFetchBlob.android.actionViewIntent(res.path(), '/') }) `

It works to me. good luck.

Worked for me! Thanks

TheAlmightyBob commented 4 years ago

This is due to a bug in RNFetchBlobReq.java's onReceive.

It throws this error if path was not provided and it has not retrieved a filePath from the DownloadManager result:

if (options.addAndroidDownloads.hasKey("path")) {
...
}
else {
    if(filePath == null)
        this.callback.invoke("Download manager could not resolve downloaded file path.", RNFetchBlobConst.RNFB_RESPONSE_PATH, null);
    else
        ...
}

However, it does not bother trying to retrieve filePath (even though it is available) unless mime was set and it was set to image:

if ( contentUri != null &&
        options.addAndroidDownloads.hasKey("mime") &&
        options.addAndroidDownloads.getString("mime").contains("image")) {
    Uri uri = Uri.parse(contentUri);
    Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
    // use default destination of DownloadManager
    if (cursor != null) {
        cursor.moveToFirst();
        filePath = cursor.getString(0);
        cursor.close();
    }
}

I don't have time to submit a PR right now, but I believe this would be trivial to fix by changing the logic to:

if ( contentUri != null ) {
    Uri uri = Uri.parse(contentUri);
    Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Files.FileColumns.DATA}, null, null, null);

The path check in that first code snippet is why adding that property to your config can work around the issue.

(also this was previously reported in the original project: https://github.com/wkh237/react-native-fetch-blob/issues/606)

binotm25 commented 4 years ago

I fixed this issue by including the path inside the addAndroidDownloads object.

RNFetchBlob.config({ fileCache : true, path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', addAndroidDownloads : { notification : true, useDownloadManager: true, title : 'test-report-'+data.id+'.pdf', mime : 'application/pdf', description : 'Your test reports.', path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', <--- Right here } })

After that the res.path() returns the right location and I can open the pdf file which I have downloaded.

spsingh559 commented 4 years ago

I fixed this issue by including the path inside the addAndroidDownloads object.

RNFetchBlob.config({ fileCache : true, path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', addAndroidDownloads : { notification : true, useDownloadManager: true, title : 'test-report-'+data.id+'.pdf', mime : 'application/pdf', description : 'Your test reports.', path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', <--- Right here } })

After that the res.path() returns the right location and I can open the pdf file which I have downloaded.

There is no need to add path and file cache in config field, Download manager ignore both the fields. Having path defined in addAndroidDownloads is enough to work.

armendu commented 3 years ago

I fixed this issue by including the path inside the addAndroidDownloads object.

RNFetchBlob.config({ fileCache : true, path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', addAndroidDownloads : { notification : true, useDownloadManager: true, title : 'test-report-'+data.id+'.pdf', mime : 'application/pdf', description : 'Your test reports.', path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', <--- Right here } })

After that the res.path() returns the right location and I can open the pdf file which I have downloaded.

this one worked for me. Thank you very much

abbasmoosavi commented 3 years ago

I fixed this issue by including the path inside the addAndroidDownloads object.

RNFetchBlob.config({ fileCache : true, path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', addAndroidDownloads : { notification : true, useDownloadManager: true, title : 'test-report-'+data.id+'.pdf', mime : 'application/pdf', description : 'Your test reports.', path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', <--- Right here } })

After that the res.path() returns the right location and I can open the pdf file which I have downloaded.

Nice, safe my time, thank you

gift-sequoiaat commented 1 year ago

I fixed this issue by including the path inside the addAndroidDownloads object. RNFetchBlob.config({ fileCache : true, path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', addAndroidDownloads : { notification : true, useDownloadManager: true, title : 'test-report-'+data.id+'.pdf', mime : 'application/pdf', description : 'Your test reports.', path : dirs.DownloadDir + '/test-report-'+data.id+'.pdf', <--- Right here } }) After that the res.path() returns the right location and I can open the pdf file which I have downloaded.

Nice, safe my time, thank you

This solution works thanks