Android原生加载显示在线PDF链接

工作需求,需要加载一个在线pdf,链接以“.pdf"结尾。通过查找,大都需要先把pdf文件下载下来,然后再加载,有以下几种实现方式:

方法一 :参考谷歌demo(android-PdfRendererBasic

这个方案的好处是对apk安装包的体积基本无影响;
缺点 ①遇到PDF文件过大的时候可能OOM ②只能查看PDF文件,无法拓展,如果后续出现Word文档则无法满足。

方法二:使用JS 处理支持webview阅读pdf

https://github.com/mozilla/pdf.js/下下来放到项目的assets下面,然后将这些copy到data下或者sd卡中,pdf也下载到相对目录下,然后就可以同上一样作为本地服务器一样阅读pdf了
缺点:

  1. 包增大几兆。
  2. 展示出来的样式太复杂,类似pdf工具的界面,太丑了。
  3. 无法展示水印

方法三:使用第三方SDK(比如:PdfViewPagerAndroidPdfViewerandroid-pdfview

AndroidPdfViewer:
优点:完美展示
缺点:使用后apk包增大15兆(真心没法接受)

android-pdfview:
优点:正常展示
缺点:

  1. 包大小增加三、四兆(勉强可以接受)。
  2. 水印无法展示出来(硬伤啊)

方法四:使用腾讯浏览服务

优点:

  1. 完美展示,水印也可以展示出来
  2. 包大小增加只有几十K

经过多次尝试,选择了方法四的解决方案:
代码如下

 /**
     * 使用x5内核加载pdf
     */
    private void loadPdf() {
        tbsReaderView = new TbsReaderView(this, this);
        LinearLayout rootRl = (LinearLayout) findViewById(R.id.root);
        rootRl.addView(tbsReaderView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));


        new LoadPDFAsyncTask(pdfName, new LoadPDFAsyncTask.OnLoadPDFListener() {
            @Override
            public void onCompleteListener(File file) {

                Bundle bundle = new Bundle();
                bundle.putString("filePath", file.getPath());
                bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
                boolean result = tbsReaderView.preOpen("pdf", false);
                if (result) {
                    tbsReaderView.openFile(bundle);
                    mLoadingDialog.dismiss();
                }

            }

            @Override
            public void onFailureListener() {
                ToastUtil.showToast(mContext, "加载失败");
                mLoadingDialog.dismiss();
            }

            @Override
            public void onProgressListener(Integer curPro, Integer maxPro) {

            }
        }).execute(webUrl);
    }

下载工具类

public class LoadPDFAsyncTask extends AsyncTask<String, Integer, File> {
    private OnLoadPDFListener onLoadPDFListener;
    private String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator + "zxp" + File.separator + "pdf";
    private String fileName;

    public LoadPDFAsyncTask(String fileName) {
        this.fileName = fileName;
    }

    public LoadPDFAsyncTask(String fileName, OnLoadPDFListener onLoadPDFListener){
        this.fileName = fileName;
        this.onLoadPDFListener = onLoadPDFListener;
    }

    public void setOnLoadPDFListener(OnLoadPDFListener onLoadPDFListener) {
        this.onLoadPDFListener = onLoadPDFListener;
    }

    //这里是开始线程之前执行的,是在UI线程
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    //当任务被取消时回调
    @Override
    protected void onCancelled() {
        super.onCancelled();
        if (onLoadPDFListener != null) onLoadPDFListener.onFailureListener();
    }

    //当任务被取消时回调
    @Override
    protected void onCancelled(File file) {
        super.onCancelled(file);
    }

    //子线程中执行
    @Override
    protected File doInBackground(String... strings) {
        String httpUrl = strings[0];
        if (TextUtils.isEmpty(httpUrl)) {
            if (onLoadPDFListener != null) onLoadPDFListener.onFailureListener();
        }

        File pathFile = new File(filePath);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }

        File pdfFile = new File(filePath + File.separator + fileName);
        if (pdfFile.exists()) {
            return pdfFile;
        }
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            int count = conn.getContentLength(); //文件总大小 字节
            int curCount = 0; // 累计下载大小 字节
            int curRead = -1;// 每次读取大小 字节
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream is = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(pdfFile);
                byte[] buf = new byte[1024];
                while ((curRead = is.read(buf)) != -1) {
                    curCount += curRead;
                    fos.write(buf, 0, curRead);
                    publishProgress(curCount, count);
                }

                fos.close();
                is.close();
                conn.disconnect();
            }


        } catch (Exception e) {
            e.printStackTrace();
            if (onLoadPDFListener != null) onLoadPDFListener.onFailureListener();
            return null;
        }

        return pdfFile;
    }

    //更新进度
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        if (onLoadPDFListener != null) {
            onLoadPDFListener.onProgressListener(values[0], values[1]);
        }
    }

    //当任务执行完成是调用,在UI线程
    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);
        if (onLoadPDFListener != null) {
            if (file != null && file.exists()) {
                onLoadPDFListener.onCompleteListener(file);
            } else {
                onLoadPDFListener.onFailureListener();
            }
        }

    }


    //下载PDF文件时的回调接口
    public interface OnLoadPDFListener {
        //下载完成
        void onCompleteListener(File file);
        //下载失败
        void onFailureListener();
        //下载进度 字节
        void onProgressListener(Integer curPro, Integer maxPro);
    }

需要注意的地方:

使用腾讯浏览服务,需要载加载界面退出的时候调用一个方法,否则下次加载时会一直提示”加载中“

 @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tbsReaderView != null) {
            tbsReaderView.onStop();
        }
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
回答: 在Android中,有多种方法可以处理PDF文件。其中一种方法是使用第三方库来加载显示PDF文件。在引用\[1\]中的代码示例中,使用了一个名为PDFView的控件来显示PDF文件。通过设置PDFView的属性和调用loadOnlinePDF方法,可以加载显示指定URL的PDF文件。另外,在引用\[2\]中提到了使用ScrollView和PDFView来实现滑动效果,可以在PDF区域内滑动时先滑动PDF,滑动到底部后再滑动整个ScrollView。这种方法可以解决PDF文件在滑动过程中的显示问题。此外,还可以使用Android原生自带的PDF管理库,其中包括pdfRenderpdfDocument两个类。但是需要注意的是,这种方法只能在Android 5.0及以上的版本中使用。 #### 引用[.reference_title] - *1* *2* [Android应用内加载pdf 使用pdf.js](https://blog.csdn.net/a1466850214/article/details/123922246)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android在线预览pdf文件的几种方式](https://blog.csdn.net/loveliqi/article/details/124143369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值