尝试下载pdf文件但收到downloadManager.enqueue错误。无法阅读ContentResolver.insert

时间:2017-09-16 12:34:09

标签: android android-contentresolver android-download-manager

下载pdf文件但在dm1.enqueue(请求)时收到错误.it需要WRITE_EXTERNAL_STORAGE权限。但是我已经添加了这个权限。 请检查方法和错误日志

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)

1 个答案:

答案 0 :(得分:0)

替换

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

作者

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