Im using the Download Manager API to download files from a server through a HTTP Request. This method works perfectly file for Android API version < 11. I need to implement it for Android API version 10 (GingerBread). In this one, it is giving ERROR_FILE_ERROR. Please advice.
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);
}
It gives Download Failed, ERROR_FILE_ERROR
It's been some time, but just for the record. This also happened to me on a Nexus 4 with Android KitKat 4.4.4. However, on a Nexus 5 & 6 with Android Lollipop I do not see this error. In my case, the solution was to remove the folder name from the second parameter in
setDestinationInExternalPublicDir
. Instead of this:Use this: