【发布时间】:2014-09-07 10:37:17
【问题描述】:
我使用 Android 中的 DownloadManager API 成功下载了一个 pdf 文件。
清单权限设置正确。 文件下载正确。
但是当它试图打开它时却显示“无法打开文件”。
请帮忙打开下载的文件。我想我没有为文件设置正确的名称和扩展名。如何设置?
private void DownloadBook(String url, String title){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//request.setDescription("Some descrition");
String tempTitle = title.replace(" ","_");
request.setTitle(tempTitle);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, tempTitle+".pdf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
request.setMimeType(".pdf");
request.allowScanningByMediaScanner();
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
manager.enqueue(request);
}
【问题讨论】:
-
请显示您尝试确定文件名并打开文件的代码。
-
我没有尝试从我的应用程序中打开文件。 pdf 将下载到下载文件夹中,然后用户从那里手动打开它。没有打开下载的文件。
-
您在 if 循环内和倒数第三行复制了
request.allowScanningByMediaScanner();行。
标签: android android-webview android-download-manager