简体   繁体   English

无法打开通过下载管理器 api 下载的文件

[英]can't open file downloaded through download manager api

I successfully downloaded a pdf file using DownloadManager API in android.我使用 android 中的 DownloadManager API 成功下载了 pdf 文件。

Manifest permissions are set correctly.清单权限设置正确。 File downloaded correctly.文件下载正确。

But when it is tried to open it says "can't open file".但是当它试图打开时,它说“无法打开文件”。

Please help to open the downloaded file.请帮忙打开下载的文件。 I guess I was failing to set the proper name and extension for the file.我想我没有为文件设置正确的名称和扩展名。 How to set it?如何设置?

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);
}

Problem solved. 问题解决了。 The problem is in setting the MIME type for the downloaded file. 问题在于为下载的文件设置MIME类型。 By googling by default the server sends the file as its content type as application/x-download instead of application/pdf. 默认情况下,通过谷歌搜索,服务器将文件作为其内容类型发送为application / x-download而不是application / pdf。 So in the set mime type as pdf. 所以在设置mime类型为pdf。

I changed this request.setMimeType(".pdf"); 我更改了这个request.setMimeType(".pdf"); to request.setMimeType("application/pdf"); request.setMimeType("application/pdf"); that's it. 而已。

request.setMimeType() for different types of files on Kotlin if "can't open file" android DownloadManager request.setMimeType()用于Kotlin上的不同类型的文件,如果“无法打开文件”android DownloadManager

val downloadFile = download   // for example text.txt, text.xml, icon.jpg...
request.setMimeType(getMimeFromFileName(downloadFile))

private fun getMimeFromFileName(fileName: String): String? {
        val map = MimeTypeMap.getSingleton()
        val ext = MimeTypeMap.getFileExtensionFromUrl(fileName)
        return map.getMimeTypeFromExtension(ext)
    }

As the documentation says - setMimeType() will override the content type declared in the server's response.正如文档所说 - setMimeType() will override the content type declared in the server's response. Therefore, it should be taken into account that once we are sure that the server is returning information about the extension, we do not have to set it ourselves - to avoid a mismatch.因此,应该考虑到,一旦我们确定服务器正在返回有关扩展的信息,我们就不必自己设置它 - 以避免不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM