joltup / rn-fetch-blob

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

Downloads using Android download manager are causing a crash on Android 10 #481

Open sattaman opened 4 years ago

sattaman commented 4 years ago

I am using

"rn-fetch-blob": "0.10.15"
"react-native": "0.59.9",

My setup is as follows:

return RNFetchBlob.config({
    fileCache: true,
    path: `${RNFetchBlob.fs.dirs.DownloadDir}/${filename}.${ext}`,
    addAndroidDownloads: {
      notification: true,
      title,
      mime: mimeType,
      mediaScannable: true,
    },
  }).fetch('..

This code has been running in production for a while with no issues, but is crashing on Android 10 with the following error:

019-11-01 14:03:17.035 15053-15070/android.process.media E/DatabaseUtils: Writing exception to parcel java.lang.IllegalArgumentException: File doesn't exist: /data/data/com.me.myapp/files/heren-cgm-pdf-20191028.pdf at com.android.providers.downloads.DownloadProvider.checkDownloadedFilePath(DownloadProvider.java:1103) at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:719) at android.content.ContentProvider$Transport.insert(ContentProvider.java:309) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:154) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994) 2019-11-01 14:03:17.041 19986-20176/com.me.myapp E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.me.myapp, PID: 19986 java.lang.IllegalArgumentException: File doesn't exist: /data/data/com.me.myapp/files/heren-cgm-pdf-20191028.pdf at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:481) at android.content.ContentResolver.insert(ContentResolver.java:1828) at android.app.DownloadManager.addCompletedDownload(DownloadManager.java:1528) at android.app.DownloadManager.addCompletedDownload(DownloadManager.java:1398) at com.RNFetchBlob.RNFetchBlobReq$3.onResponse(RNFetchBlobReq.java:443) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:919)

As a temporary work around , I have found removing addAndroidDownloads completely stops the crash.

sattaman commented 4 years ago

I'm going to have a go at fixing this , any pointers would be received gratefully , as I'm no java dev !

sattaman commented 4 years ago

I think I've found the issue and how to get it working, useDownloadManager needed adding (although up until now it has worked without), and I also needed to duplicate the path (undocumented I think?) . Is this the expected behaviour? I can do a pr to update the docs if so? Or should the path for the android downloads be being passed down from the root config?

return RNFetchBlob.config({
    fileCache: true,
    path: `${RNFetchBlob.fs.dirs.DownloadDir}/${filename}.${ext}`,
    addAndroidDownloads: {
      path: `${RNFetchBlob.fs.dirs.DownloadDir}/${filename}.${ext}`, // path needed duplicating here
      useDownloadManager: true, // without this it works < android 10 , but crashes in android 10
      notification: true,
      title,
      mime: mimeType,
      mediaScannable: true,
    },
  }).fetch(...
lorenzoangelini commented 4 years ago

Hi i resolved this issue adding: <application .... android:requestLegacyExternalStorage="true"

to the manifest.

Here the explication: https://stackoverflow.com/questions/56821095/android-q-file-mkdirs-returns-false