尝试下载pdf文件,但获取下载manager.enqueue错误.无法读取contentResolver.insert



下载pdf文件时,但是在dm1.enqueue(request)中获得错误。但是我已经添加了此许可。请检查方法和错误日志

private void downloadFileInTask1(Context v, String url) {
    if (TextUtils.isEmpty(url)) {
        throw new IllegalArgumentException("url cannot be empty or null");
    }

    String resolvedFile = getPDFname(url, "unknown_file");//resolveFile(url, "unknown_file");
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (isExternalStorageWritable()) {
        File file = new File(v.getExternalFilesDir(null), resolvedFile);
        Uri destinationUri = Uri.fromFile(file);
        destinationUri1 = file;
        request.setMimeType("application/pdf");
        request.setDestinationUri(destinationUri);
        dm1.enqueue(request);
    }
}

错误日志:

java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 11110 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1489)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:468)
at android.content.ContentResolver.insert(ContentResolver.java:1212)
at android.app.DownloadManager.enqueue(DownloadManager.java:1390)
at com.srllimited.srl.ShowWebView.downloadFileInTask(ShowWebView.java:560)
at com.srllimited.srl.ShowWebView.access$100(ShowWebView.java:51)
at com.srllimited.srl.ShowWebView$2$1.run(ShowWebView.java:116)
at java.lang.Thread.run(Thread.java:841)

替换

File file = new File(v.getExternalFilesDir(null), resolvedFile);

final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), resolvedFile);

最新更新