12

Possible duplicate

I am downloading large zip files using Android DownloadManager. I have listview that shows list of all zip files and user can tap item to start downloading. Only one item can be downloaded at a time. When new list item starts downloading while other download is in progress i remove previous download id from queue. Everything is working fine. But sometimes i am getting ERROR_FILE_ERROR on LG g2 device OS 5.0.2. Here is code:

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);

It is working fine on other devices including nexus 5, samsung s3, note2, huawei etc. When i start downloading a file it instantly stops/failed with reason DownloadManager.ERROR_FILE_ERROR. I have tried to remove/clean external storage directory, making sure that its not ERROR_INSUFFICIENT_SPACE error etc. but it didn't work. Any help?

Community
  • 1
  • 1
Nouman Bhatti
  • 1,777
  • 4
  • 28
  • 55

1 Answers1

3

This might be a bit more bullet-proof:

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);
}
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54