Opening a file downloaded with DownloadManager
Asked Answered
H

0

1

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

The filename I pass to that method is received from DownloadManager query method:

@Override
public void onReceive(Context context, Intent intent) {
    long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

    if (myDownloadreference == reference) {
        Log.d(TAG, "onReceive  descarga es la nuestra");
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(reference);
        Cursor cursor = mDownloadManager.query(query);
        cursor.moveToFirst();
        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
        //This is String I pass to openFile method
        String savedFilePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
        ...
    }
}

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 files with spaces on its name. It always shows the Toast (even if apps for that mime type 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 files with spaces on its name 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

EDIT

After @CommonsWare advices, It is working now. Problem was with received param I was using from DownloadManager. Correct file name to use in my openFile() method is

cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

Instead of:

cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
Honolulu answered 5/5, 2015 at 14:53 Comment(6)
Try a PDF with a simpler name, like foo.pdf.Telespectroscope
Ey @Telespectroscope you are a legend on Android Development! After trying that, It seems to be a problem while opening files with spaces on the name. It is not related with file Mime Type. I'm modifying question right now.Honolulu
"After trying that, It seems to be a problem while opening files with spaces on the name" -- I was thinking of something along those lines. Where is fileName coming from?Telespectroscope
It is the name I receive from DownloadManager after querying: Cursor cursor = mDownloadManager.query(query); cursor.moveToFirst(); String savedFilePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));Honolulu
You could try COLUMN_LOCAL_URI instead, to avoid extra converting on your side, and see if that helps.Telespectroscope
great @Telespectroscope It is working like a charm using COLUMN_LOCAL_URI.Honolulu

© 2022 - 2024 — McMap. All rights reserved.