可能重复
我正在使用Android DownloadManager下载大型zip文件。我有listview,显示所有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错误等,但它不起作用。任何帮助吗?
这可能更防弹一些:
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);
}