Kal*_*aju 6 android android-webview android-download-manager
我在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);
}
Run Code Online (Sandbox Code Playgroud)
问题解决了。问题在于设置下载文件的MIME类型。默认情况下,通过谷歌搜索,服务器会将文件作为其内容类型作为application / x-download而不是application / pdf发送。因此在设置mime类型为pdf时。
我改变了这种request.setMimeType(".pdf");对request.setMimeType("application/pdf");
,就是这样。