mardi 5 mai 2015

Opening a file downloaded with DownloadManager

I'm trying to open a file downloaded using DownloadManager. I'm getting the local filename after download is finished. After that I suggest my user to open file (or decline). The method I use for opening file is:

private void openFile(String fileName) {
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl((fileName));
    String type = map.getMimeTypeFromExtension(ext);

    Intent install = new Intent(Intent.ACTION_VIEW);
    Log.d(TAG, "openFile Trying to open file: " + Uri.fromFile(new File(fileName)));
    install.setDataAndType(Uri.fromFile(new File(fileName)), type);
    try {
        mCx.startActivity(install);
    } catch (Exception e) {
        //Si no hay ninguna app capaz de abrir el archivo, fallará.
        e.printStackTrace();
        Toast.makeText(mCx, mCx.getString(R.string.sin_app_archivo), Toast.LENGTH_LONG).show();
    }
}

It usually works with most of filetype. If there isn't any app able to open it, it will show a Toast.

The strange behaviour happens with pdf files. It always shows the Toast (even if PDF apps are installed) when I execute my openFile method. Nevertheless, by clicking on DownloadManager notification, Android open PDF file (or shows App picker if two or more Apps can handle file opening). So, what is the Intent that SO launches when user clicks on notification? How can I change my method in order to open pdf files also?

An example of file that can be openned with openFile method:

D/GdDocumentacionDownloaderHelper﹕ openFile Trying to open file: file:///storage/emulated/0/Android/data/es.ineco.app/files/Download/ic_action_delete-11.zip

And another example of file that can't be openned with openFile method:

D/GdDocumentacionDownloaderHelper﹕ openFile Trying to open file: file:///storage/emulated/0/Android/data/es.ineco.app/files/Download/10-02_Prevbat_363%2B570_363%2B820_V1%20(1)-5.pdf

Aucun commentaire:

Enregistrer un commentaire