1

Using the DownloadManager, I am trying to download files from my app but it is failing to download any files. This issue I have recreated on multiple devices all API 16 or below. Anything above that API works fine.

What happens is:

  1. I click the file to download in my app
  2. It queues up the download and shows the persistent notification
  3. The progress bar keeps going and fails after a certain time, usually a couple of minutes.

This happens every time! Please help!

private static void downloadFile(BaseActivity activity, FileInfo fileContent) {
        if (!DownloadManagerUtil.isDownloadManagerEnabled(activity)) {
            DownloadManagerDialog.newInstance().show(activity.getSupportFragmentManager(),
                    DownloadManagerDialog.TAG);
            return;
        }

        if (!FileUtil.hasStorage(true)) {
            ToastUtil.showLong(R.string.external_storage_error);
            return;
        }

        try {
            final DownloadManager.Request request = DownloadManagerUtil.createRequest(
                    Uri.parse(fileContent.getResourceUrl()), fileContent.getTitle());
            final DownloadManager downloadManager
                    = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
            downloadManager.enqueue(request);
            ToastUtil.showShort(R.string.download_notification);
        } catch (IllegalArgumentException e) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                ToastUtil.showLong(R.string.download_not_supported_on_old_androids);
            } else {
                throw new IllegalArgumentException(e);
            }
        }
    }

public static DownloadManager.Request createRequest(Uri uri, String fileName) {
        final DownloadManager.Request request = new Request(uri);
        request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        request.setTitle(fileName);

        final File fullPath = new File(FileUtil.getDownloadFolderFullPath());
        if (!fullPath.exists() && !fullPath.isDirectory()) {
            fullPath.mkdirs();
        }

        final String path = FileUtil.getDownloadFolder();
        request.setDestinationInExternalPublicDir(path, fileName);

        if (DeviceUtil.isNewerThanGingearBread()) {
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.allowScanningByMediaScanner();
        }
        return request;
    }
The Nomad
  • 7,155
  • 14
  • 65
  • 100

0 Answers0