Android Download PDF file and Save to Downloads folder and Open it with a PDF Viewer

Android Download PDF file and Save to Downloads folder and Open it with a PDF Viewer

Download PDF file and Store it to Downloads folder

DownloadManager downloadmanager;
    Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .mkdirs();

    downloadmanager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
    String url = hardcode + bb ;
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri)
            .setTitle(bb )
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    bb)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Log.i("Download1", String.valueOf(request));
    downloadmanager.enqueue(request);

Open PDF file from Downloads folder and show it on a PDF viewer

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
            "YOUR FILE NAME");
    Uri path = Uri.fromFile(file);
    Log.i("Fragment2", String.valueOf(path));
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        this.startActivity(pdfOpenintent);
    } catch (ActivityNotFoundException e) {

    }

Leave a Reply

Your email address will not be published. Required fields are marked *