无法下载视频,但为其工作的下载管理器pdf

时间:2014-07-22 09:14:49

标签: java android download-manager

您好我正在尝试通过DownloadManager下载视频文件和pdf。对于pdf它的工作正常,但当我试图下载视频时,它没有下载。

我正在使用以下代码:

private void downloadFileFromServer(String fid, String title, String uri) {
    boolean isPdf = uri.matches(".*\\b.pdf\\b.*");
    if (isPdf) {
        fileName = fid + ".pdf";
    } else {
        fileName = fid + ".mp4";
    }
    File file = new File(getActivity().getExternalFilesDir("DDocs/")
            + "/Files/" + fileName);
    if (file.exists()) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (isPdf) {
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "video/mp4");
        }
        startActivity(intent);
    } else if (BUtil.isOnline(getActivity())) {
        downloadManager = (DownloadManager) getActivity().getSystemService(
                DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse(uri);
        DownloadManager.Request request = new DownloadManager.Request(
                Download_Uri);
        request.addRequestHeader(BConstant.WEB_SERVICES_COOKIES,
                cookie);
        request.addRequestHeader(BConstant.WEB_SERVICES_TOKEN_HEADER,
                token);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                | DownloadManager.Request.NETWORK_MOBILE);
        request.setTitle(title);
        request.setDestinationInExternalFilesDir(getActivity(),
                "DDocs/Files", fileName);

        if (!file.exists()) {
            progressBar = new ProgressDialog(getActivity());
            progressBar.setCancelable(false);
            progressBar.setMessage(BConstant.LODING);
            progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressBar.show();
            lastDownload = downloadManager.enqueue(request);

        }
    } else if (!BUtil.isOnline(getActivity())) {
        ToastUserMessage.message(getActivity(),
                BUserMessage.FIRST_TIME_LOAD_MESSAGE);
    }
}

0 个答案:

没有答案