Android DownloadManager“无法打开文件” [英] Android DownloadManager "Impossible to open file"

查看:289
本文介绍了Android DownloadManager“无法打开文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DownloadManager从webService下载文件。下载成功完成,但是当我尝试在下载文件夹中打开新文件时,出现此错误无法打开文件(而且我知道我可以打开这种类型的文件)。

I'm using DownloadManager to download a file from a webService. The download end successfuly, but when I try to open the new file in the "download" folder I've got this error "Impossible to open file" (and I know I can open this type of file).

此外,当我将手机插入计算机并用其打开下载文件时,文件会成功打开并且没有损坏。

Also, when I plug my phone to my computer and when I open the download file with it, the file open successfuly and is not corrupted.

我没有其他错误,所以我真的迷路了!

I don't have other error, so I'm really lost !

这里是我的代码:

    /*Data*/
    int filePosition = position - _subFolderNameList.length;
    String url = _folder.getFiles().get(filePosition).getUrl();
    String FileName = _folder.getFiles().get(filePosition).getName();
    String Description = _folder.getFiles().get(filePosition).getUrl();

    /*Prepare request*/
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription(Description);
    request.setTitle(FileName);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileName);

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request); // Send request

编辑:清单中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />


推荐答案

好的,我发现了问题!

我必须使用 setMimeType()

public DownloadManager.Request setMimeType (String mimeType);

这篇关于Android DownloadManager“无法打开文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆