Opening a file downloaded with DownloadManager

大憨熊 提交于 2019-12-11 11:59:01

问题


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

来源:https://stackoverflow.com/questions/30056390/opening-a-file-downloaded-with-downloadmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!