AndroidUtilCode
AndroidUtilCode copied to clipboard
UriUtils.uri2File, NullPointerException
Describe the bug 调用系统下载器下载完成后, 在广播接收器中执行以下java代码
The version of utilcode: 1.19.4
The code:
Uri downloadFileUri = dManager.getUriForDownloadedFile(myDownloadID);
File file = UriUtils.uri2File(downloadFileUri, MediaStore.Files.FileColumns.DATA);//"_data"//MediaStore.Images.Media.DATA//第二个参数是否传错了
AppUtils.installApp(file);
The stack of crash:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference
at com.blankj.utilcode.util.UriUtils.uri2File(UriUtils.java:56)
at xxx.UpdateBroadcastReceiver.onReceive(UpdateBroadcastReceiver.java:44)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2778)
... 9 more

直接 uri.getPath(); 就行
你这原因可能是下载完后没有插入到媒体数据库里所以扫描不到吧
谢谢, uri.getPath()能用