安卓下载管理器.ERROR_FILE_ERROR

12

可能重复

我正在使用 Android DownloadManager 下载大型 zip 文件。我有一个列表视图,显示所有 zip 文件的列表,用户可以点击项目开始下载。一次只能下载一个项目。当新的列表项开始下载,而另一个下载正在进行时,我会从队列中移除以前的下载 id。一切都运行良好。但是,在 LG G2 设备 OS 5.0.2 上,有时候我会收到 ERROR_FILE_ERROR 错误。这是代码:

Uri uri = Uri.parse(path);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setVisibleInDownloadsUi(false);

String localStorageBasePath = FileUtils.zipDirectory(context).getAbsolutePath() + File.separator + fileName;
Uri localStorageBasePathUri = Uri.fromFile(new File(localStorageBasePath));
request.setDestinationUri(localStorageBasePathUri);
Long downloadId = downloadManager.enqueue(request);

其他设备(包括Nexus 5,三星S3,Note2,华为等)都可以正常运行。但是当我开始下载文件时,它立即停止并显示错误信息DownloadManager.ERROR_FILE_ERROR。我已经尝试过清除外部存储目录、确保它不是ERROR_INSUFFICIENT_SPACE错误等,但没有效果。有什么帮助吗?

1个回答

3
这可能更加健壮可靠:
public void file_download(String path, String filename) 
{
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();

    Uri uri = Uri.parse(path);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setDescription("");
    request.setTitle("");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    {
       request.allowScanningByMediaScanner();
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setVisibleInDownloadsUi(false);
    request.setDestinationInExternalFilesDir(context, DIRECTORY_DOWNLOADS, fileName);

    //String localStorageBasePath = FileUtils.zipDirectory(context).getAbsolutePath() + File.separator + fileName;
    //Uri localStorageBasePathUri = Uri.fromFile(new File(localStorageBasePath));
    //request.setDestinationUri(localStorageBasePathUri);
    DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
    Long downloadId = downloadManager.enqueue(request);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接