1

I'm using this source code to download a file in the public and external "downloads" directory of android.

private void download(String url){
    if (ActivityCompat.checkSelfPermission(SectionManager.getInstance().getCurrentActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        PermissionManager.getInstance().requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PermissionManager.PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        DownloadManager.Request r = new DownloadManager.Request(Uri.parse(url));

        // This put the download in the same Download dir the browser uses
        //r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url.substring(url.lastIndexOf('/')+1));
        r.setDestinationInExternalFilesDir(SectionManager.getInstance().getCurrentActivity(), Environment.DIRECTORY_DOWNLOADS, url.substring(url.lastIndexOf('/')+1));

        // When downloading music and videos they will be listed in the player
        // (Seems to be available since Honeycomb only)
        r.allowScanningByMediaScanner();

        // Notify user when download is completed
        // (Seems to be available since Honeycomb only)
        r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        // Start download
        DownloadManager dm = (DownloadManager) SectionManager.getInstance().getCurrentActivity().getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(r);
    }
}

When url is http://mobimento.com/~psaez/video2.zip it gives this error on logcat

2019-06-17 19:15:01.916 11623-19923/? D/DownloadManager: [191] Starting
2019-06-17 19:15:02.308 11623-19923/? W/DownloadManager: [191] Stop requested with status HTTP_DATA_ERROR: Failed reading response: java.net.ProtocolException: unexpected end of stream
2019-06-17 19:15:02.310 11623-19923/? D/DownloadManager: [191] Finished with status WAITING_TO_RETRY

But after 30-40 seconds and sometimes after 1 or 2 minutes, the download starts and finishes correctly:

2019-06-17 19:17:42.383 11623-20295/? D/DownloadManager: [192] Starting
2019-06-17 19:17:42.487 11623-20295/? D/DownloadManager: [192] Finished with status SUCCESS

What is wrong in the code? Why it gives an error and after 30-40 seconds it starts downloading?

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • The first one fails because there is nothing at that URL. You can test that by clicking the link. The second one will have problems on Android 9.0+ due to the cleartext protocol (`http` instead of `https`), though your error suggests that you are running on an older device (or opted into cleartext communications). Have you tried MP4 URLs on other servers? – CommonsWare Jun 17 '19 at 16:13
  • try the solutions answered and let me know if it works or not – Quick learner Jun 17 '19 at 16:27
  • 1
    @CommonsWare sorry the link was broken, I updated the question with the correct video and error. – NullPointerException Jun 17 '19 at 17:16

0 Answers0