flutter_downloader icon indicating copy to clipboard operation
flutter_downloader copied to clipboard

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getPath()' on a null object reference

Open OsamaAldawoody opened this issue 3 years ago • 14 comments

This is the consol message when I tried to download task


Backoff delay duration less than minimum value
I/flutter (22485): f774957b-612b-4aac-857a-8c817e3f8e33
D/EGL_emulation(22485): eglCreateContext: 0xf4066ff0: maj 2 min 0 rcv 2
D/EGL_emulation(22485): eglCreateContext: 0xf4066960: maj 2 min 0 rcv 2
D/HostConnection(22485): HostConnection::get() New Host Connection established 0xf4068090, tid 22562
D/HostConnection(22485): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
D/EGL_emulation(22485): eglMakeCurrent: 0xf4066960: ver 2 0 (tinfo 0xb32a4450) (first time)
D/PathProviderPlugin(22485): Use TaskQueues.
D/DownloadWorker(22485): DownloadWorker{url=[https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf,filename=null,savedDir=/storage/emulated/0/Download,header=,isResume=false,status=1]()
D/DownloadWorker(22485): Update notification: {notificationId: 5, title: [https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf](), status: 2, progress: 0}
D/DownloadWorker(22485): Open connection to [https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf]()
D/DownloadWorker(22485): Content-Type = application/pdf
D/DownloadWorker(22485): Content-Length = 4186004
D/DownloadWorker(22485):  Charset = null
D/DownloadWorker(22485): Content-Disposition = null
D/DownloadWorker(22485): fileName = flutter-succinctly.pdf
E/DownloadWorker(22485): It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'
D/DownloadWorker(22485): Update notification: {notificationId: 5, title: [https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf](), status: 4, progress: -1}
W/System.err(22485): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getPath()' on a null object reference
W/System.err(22485): 	at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:398)
W/System.err(22485): 	at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:235)
W/System.err(22485): 	at androidx.work.Worker$1.run(Worker.java:86)
W/System.err(22485): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(22485): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(22485): 	at java.lang.Thread.run(Thread.java:923)
I/WM-WorkerWrapper(22485): Worker result FAILURE for Work [ id=f774957b-612b-4aac-857a-8c817e3f8e33, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]
I/flutter (22485): Fatal: could not find callback
D/HostConnection(22485): HostConnection::get() New Host Connection established 0xf406b4a0, tid 22560
F/libc    (22485): FORTIFY: pthread_mutex_lock called on a destroyed mutex (0xc4931d28)
F/libc    (22485): Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 22516 (1.ui), pid 22485 (ample.task_flow)
Lost connection to device.


OsamaAldawoody avatar Mar 02 '22 12:03 OsamaAldawoody

It looks like you are trying to save files in public storage but not setting 'saveInPublicStorage' to 'true'. Set saveInPublicStorage as true in flutterDownloader.

Example: FlutterDownloader.enqueue( url: 'YOUR_URL', savedDir: 'YOUR_PATH', saveInPublicStorage: true, showNotification: true);

selvas1998 avatar Mar 04 '22 12:03 selvas1998

+1

selvam920 avatar Mar 05 '22 04:03 selvam920

This problem occurs when the file already exists

xuelongqy avatar Mar 21 '22 03:03 xuelongqy

This problem occurs when the file already exists how to check if file already exist

iamtechmanish avatar Jul 17 '22 10:07 iamtechmanish

JUST ADD THOSE LINE saveInPublicStorage: true, INTO ........................................... FlutterDownloader.enqueue( url: 'YOUR_URL', savedDir: 'YOUR_PATH', saveInPublicStorage: true, showNotification: true);

......................................... when the file already exists

BLACKCAT25896 avatar Jul 17 '22 10:07 BLACKCAT25896

veInPublicStorage: true,

I tried this but not working

iamtechmanish avatar Jul 17 '22 10:07 iamtechmanish

if we set saveInPublicStorage: true, the file will be downloaded to download folder but i have created a folder in download folder and i need to save downloaded file to that folder. any solution?

j1pishraft avatar Sep 22 '22 03:09 j1pishraft

The thing is initially file can be saved to Download subfolder but if i delete the file and try to download again ill get this issue.

It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true' D/DownloadWorker(27718): Update notification: {notificationId: 39, title: app.apk, status: 4, progress: -1} W/System.err(27718): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getPath()' on a null object reference

j1pishraft avatar Sep 22 '22 03:09 j1pishraft

@j1pishraft I am facing same issue already downloaded file re download then after show this error

Android 9 (Huawei)

KishanViramgama avatar Oct 13 '22 07:10 KishanViramgama

if dont want saveInPublicStorage: true find package file : /Users/****/Documents/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_downloader-1.9.1/android/src/main/kotlin/vn/hunghd/flutterdownloader/DownloadWorker.kt

add if (newFile.exists()) newFile.delete(); after val newFile = File(savedDir, filename)

private fun createFileInAppSpecificDir(filename: String, savedDir: String): File? {
val newFile = File(savedDir, filename)
        if (newFile.exists()) newFile.delete();
        try {
            val rs: Boolean = newFile.createNewFile()

xiaobing1024 avatar Dec 01 '22 07:12 xiaobing1024

@j1pishraft I am facing same issue already downloaded file re download then after show this error

Android 9 (Huawei)

Actually I have done some work around.

What I have noticed in order to have proper storage access on android, we need to add MANAGE_STORAGE permission into manifest file. How ever if you need to publish your app to PlayStore the app will be rejected due to that permission, so you need to provide strong reason to PlayStore in order to approve your app to have that permission.

When we set saveInPublicStorage: true the file will be downloaded to the download folder but if we delete the file and redownload any file with the same name we get error. To make it simple when we delete a file, it will be removed from the phone but the name of the file is still hold by android OS.

Solution:

So what I have done is that i added Non-regular Time Stamp to my file name for each download so the file name will be vary on each download (if the stamp is in common format like dd-mm-yyyy the problem will be remain the same, so please make sure your stamp is non-regular format).

In this way if you delete file directly from storage you will not face any issue to redownload a file with the same name, however to delete any file within the app you need to delete the file based on download TaskID not the name of the file.

So this is my work around to omit MANAGE_STORAGE permission in manifest file.

If you needed more help please update me

j1pishraft avatar Dec 01 '22 09:12 j1pishraft

This problem occurs when the file already exists

Thanks for this information. for me, i add DateTime.now().toString()to make the file name unique.

chubzchoc avatar Dec 05 '22 04:12 chubzchoc

I find the same problem,when download task exist

AhhhhDong avatar Aug 16 '23 06:08 AhhhhDong

So I guess better follow same solution

j1pishraft avatar Aug 16 '23 09:08 j1pishraft