Nou*_*tti 12 android download-manager android-download-manager
我正在使用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);
Run Code Online (Sandbox Code Playgroud)
它在其他设备上工作正常,包括nexus 5,samsung s3,note2,huawei等.当我开始下载文件时,它立即停止/失败,原因是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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |