2

Hi i am trying to download video file and pdf through DownloadManager. For pdf its working fine but when i am trying to download the video its not getting downloaded.

I am using below code:

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);
    }
}
rupesh
  • 2,865
  • 4
  • 24
  • 50

0 Answers0