我使用Download Manager API通过HTTP请求从服务器下载文件。此方法适用于Android API版本< 11.我需要为Android API版本10(GingerBread)实现它。在这一个中,它给出了ERROR_FILE_ERROR。请指教。
Uri uri = Uri.parse(serverConnection.url + serverConnection.studentSearchService + "GetAssignmentFile/" + serverConnection.connectionString + fileList.get(i).getFileId());
final DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request downloadReq = new DownloadManager.Request(uri);
downloadReq
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
// downloadReq.allowScanningByMediaScanner();
downloadReq.setMimeType(fileList.get(i).getFileType());
downloadReq.setTitle(fileList.get(i).getFileName());
downloadReq.setDescription("attachment");
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
downloadReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else
{
downloadReq.setShowRunningNotification(true);
downloadReq.setVisibleInDownloadsUi(true);
}
myDownloadReference = downloadManager.enqueue(downloadReq);
IntentFilter intentFilter= new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
receiverDownloadComplete=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String extraId= DownloadManager.EXTRA_DOWNLOAD_ID;
long reference=intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
if(reference==myDownloadReference)
{
//Toast.makeText(getActivity(),"Downloaded",Toast.LENGTH_SHORT).show();
DownloadManager.Query query= new DownloadManager.Query();
query.setFilterById(reference);
Cursor cursor=downloadManager.query(query);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status=cursor.getInt(columnIndex);
int columnReason=cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason=cursor.getInt(columnReason);
switch(status)
{
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(getActivity(),"File Saved at:"+fileSavedPath,Toast.LENGTH_SHORT).show();
break;
case DownloadManager.STATUS_FAILED:
String failedReason="";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
Toast.makeText(getActivity(),"Download failed because "+failedReason,Toast.LENGTH_SHORT).show();
break;
}
}
}
};
getActivity().registerReceiver(receiverDownloadComplete,intentFilter);
}
它提供下载失败,ERROR_FILE_ERROR
答案 0 :(得分:2)
已经有一段时间了,但仅仅是为了记录。这也发生在使用Android KitKat 4.4.4的Nexus 4上。但是,在Nexus 5& 6用Android Lollipop我没有看到这个错误。就我而言,解决方案是从setDestinationInExternalPublicDir
中的第二个参数中删除文件夹名称。
而不是:
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myFolder/fileName.txt");
使用此:
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName.txt");
答案 1 :(得分:2)
我有一些问题。 要解决此问题,请确保目标文件的父目录存在。
fileList.get(i).getParentFile().mkdirs() //make sure parent directory is exist
downloadReq.setDescription("attachment");
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());