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

java.lang.NullPointerException: uri #535

Closed
claptv opened this issue Sep 18, 2021 · 3 comments
Closed

java.lang.NullPointerException: uri #535

claptv opened this issue Sep 18, 2021 · 3 comments

Comments

@claptv
Copy link

claptv commented Sep 18, 2021

Getting a null pointer exception from ContentResolver.openAssetFileDescriptor method. I can reproduce this problem with certain files most of times.

I was running the app on SamSung S9 Plus.

at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:133)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1484)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:1234)
at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:358)
at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:208)

addFileToDownloadsApi29 method seems to return a valid uri; however, openOutputStream(uriApi29, "w") method at DownloadWorker.java:358 is causing this problem.

[Updated] I found that ContentResolver.insert fails because the same file already exists. I called File.exists before creating a new DownloadTask, but it didn't find the same file. So I added the code below to check for the same file before inserting a new row.

// Check if the same file already exists
String strId = "";
String[] projection = {"_id", "_display_name"};
Cursor cursor = contentResolver.query(
collection,
projection,
null,
null,
null);
if (cursor.moveToFirst()) {
do {
strId = cursor.getString(cursor.getColumnIndex("_id"));
String displayName = cursor.getString(cursor.getColumnIndex("_display_name"));
log("[" + strId + "] " + displayName);
if (displayName.equals(filename)) {
log("Same file found.");
Uri uri = Uri.parse(collection.toString() +
File.separator + strId);
return uri;
}
} while (cursor.moveToNext());
cursor.close();
}


outputStream = context.getContentResolver().openOutputStream(uriApi29, "w");

I can access to the image with the HTTP Url with no problem.

Here is the log:

I/System.out(14329): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(14329): (HTTPLog)-Static: isSBSettingEnabled false
D/DownloadWorker(14329): Content-Type = image/png
D/DownloadWorker(14329): Content-Length = 906615
D/DownloadWorker(14329): Charset = null
D/DownloadWorker(14329): fileName = Pursue Spray .png
D/DownloadWorker(14329): Update notification: {notificationId: 33, title: Pursue Spray .png, status: 4, progress: -1}
W/System.err(14329): java.lang.NullPointerException: uri
W/System.err(14329): at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:133)
W/System.err(14329): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1484)
W/System.err(14329): at android.content.ContentResolver.openOutputStream(ContentResolver.java:1234)
W/System.err(14329): at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:358)
W/System.err(14329): at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:208)
W/System.err(14329): at androidx.work.Worker$1.run(Worker.java:85)
W/System.err(14329): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(14329): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(14329): at java.lang.Thread.run(Thread.java:919)
I/WM-WorkerWrapper(14329): Worker result FAILURE for Work [ id=543e7685-40e8-4b7d-9848-cd27f6399210, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]
D/ConnectivityManager(14329): unregisterNetworkCallback; CallingUid : 10918, CallingPid : 14329

@AadneEM
Copy link

AadneEM commented Sep 28, 2021

I'm having this same issue, but the file doesn't already exist. Other than that it's identical except for the file type being PDF.

It worked fine before, even if the file existed. Then it saved the file as {filename}.pdf (n) where n is a integer increasing by one for each file.

Device: OnePlus 7 Por
OS: Android 10

D/DownloadWorker( 8515): DownloadWorker{url=[url],filename=null,savedDir=/storage/emulated/0/Documents,header={[headers]},isResume=false
D/DownloadWorker( 8515): Update notification: {notificationId: 1, title: [url], status: 2, progress: 0}
D/DownloadWorker( 8515): Open connection to [url]
D/DownloadWorker( 8515): Headers = {[headers]}
D/DownloadWorker( 8515): Content-Type = application/pdf
D/DownloadWorker( 8515): Content-Length = 268150
D/DownloadWorker( 8515): Charset = null
D/DownloadWorker( 8515): Content-Disposition = attachment; filename=10199.pdf
D/DownloadWorker( 8515): fileName = 10199.pdf
D/DownloadWorker( 8515): Update notification: {notificationId: 1, title: [url], status: 4, progress: -1}
W/System.err( 8515): java.lang.NullPointerException: uri
W/System.err( 8515): 	at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:133)
W/System.err( 8515): 	at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1495)
W/System.err( 8515): 	at android.content.ContentResolver.openOutputStream(ContentResolver.java:1245)
W/System.err( 8515): 	at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:358)
W/System.err( 8515): 	at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:208)
W/System.err( 8515): 	at androidx.work.Worker$1.run(Worker.java:85)
W/System.err( 8515): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err( 8515): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err( 8515): 	at java.lang.Thread.run(Thread.java:919)
I/WM-WorkerWrapper( 8515): Worker result FAILURE for Work [ id=400bf5de-a30d-4c38-9a5c-7696a8dfc6ed, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]

PS:
For the file name mentioned above, shouldn't the (n) part be before the file extension? When it's after it makes the OS not recognize which file type is it.

@bugrevealingbme
Copy link

Same as

@hnvn
Copy link
Member

hnvn commented Oct 9, 2021

Duplicate of #548

@hnvn hnvn marked this as a duplicate of #548 Oct 9, 2021
@hnvn hnvn closed this as completed Oct 9, 2021
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

4 participants