简体   繁体   中英

Open PDF if already downloaded, if not, download the PDF using download manager

I am creating an app that displays PDF. I want to create one button that will both download the PDF (if it does not exist) and open it (if it already exists). I am able to download a PDF but how do I proceed further?

public class Download {

    DownloadManager downloadManager;
    long queid;

    public Download(Context context, String downloadurl, String filename){

        downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadurl));
        request.setDestinationInExternalFilesDir(context.getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, filename);
        queid = downloadManager.enqueue(request);
    }
}

You have downloaded the pdf. Save it or move it to a fixed folder location on SDCard. In button click event, check if the pdf file is present on the same location. If it is present then you should open it in your pdf viewer activity in app.

You may find few libraries to enable viewing pdf in your app. One of the library which I use is AndroidPdfViewer . You can get help on how to integrate it in your app from the gitbhub link.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM