Android下载管理器无法在JellyBean API 16及更低版本上下载

时间:2015-11-13 19:46:37

标签: android download android-download-manager download-manager

使用DownloadManager,我正在尝试从我的应用下载文件,但无法下载任何文件。我在多个设备上重新创建了此问题API 16 or below。任何高于该API的工作都可以。

会发生什么:

  1. 我点击了要在我的应用中下载的文件
  2. 排队下载并显示持久通知
  3. 进度条在一段时间后继续运行并失败,通常是几分钟。
  4. 每次都会发生这种情况!请帮忙!

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

0 个答案:

没有答案