6

I successfully downloaded a pdf file using DownloadManager API in android.

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);
}
Kalyan Dechiraju
  • 1,219
  • 1
  • 11
  • 29
  • Please show the code where you try to determine the file name and open the file. – greenapps Jul 16 '14 at 21:25
  • Am not trying to open the file from my app. The pdf will be downloaded into downloads folder and from there user manually opens it. That downloaded file is not being opened. – Kalyan Dechiraju Jul 17 '14 at 02:58
  • You duplicated the line ```request.allowScanningByMediaScanner();``` inside your if loop and on the third to last line. – Louie Bertoncin Jun 30 '15 at 18:19

3 Answers3

14

Problem solved. The problem is in setting the MIME type for the downloaded file. By googling by default the server sends the file as its content type as application/x-download instead of application/pdf. So in the set mime type as pdf.

I changed this request.setMimeType(".pdf"); to request.setMimeType("application/pdf"); that's it.

Kalyan Dechiraju
  • 1,219
  • 1
  • 11
  • 29
1

request.setMimeType() for different types of files on Kotlin if "can't open file" 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)
    }
Sergey Milakov
  • 321
  • 3
  • 3
0

As the documentation says - 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.

Mikołaj
  • 83
  • 7