简体   繁体   English

如果已经下载,则打开PDF;否则,请使用下载管理器下载PDF。

[英]Open PDF if already downloaded, if not, download the PDF using download manager

I am creating an app that displays PDF. 我正在创建一个显示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). 我想创建一个按钮,将下载PDF(如果它不存在)并打开它(如果它已经存在)。 I am able to download a PDF but how do I proceed further? 我可以下载PDF,但如何进一步进行?

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. 您已经下载了pdf。 Save it or move it to a fixed folder location on SDCard. 保存或将其移动到SDCard上的固定文件夹位置。 In button click event, check if the pdf file is present on the same location. 在按钮单击事件中,检查pdf文件是否在同一位置。 If it is present then you should open it in your pdf viewer activity in app. 如果存在,则应在应用程序的pdf查看器活动中将其打开。

You may find few libraries to enable viewing pdf in your app. 您可能会发现很少的库可以在您的应用程序中查看pdf。 One of the library which I use is AndroidPdfViewer . 我使用的库之一是AndroidPdfViewer You can get help on how to integrate it in your app from the gitbhub link. 您可以从gitbhub链接获取有关如何将其集成到应用程序中的帮助。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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