欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android内嵌PDF预览

程序员文章站 2022-07-11 20:38:27
一、在对应模块的build.gradle文件中加入依赖 二、Activity布局Xml文件中,加入com.github.barteksc.pdfviewer.PDFView控件 三、PDFView只能预览本地文件 如果是网络PDF还需要下载 PDFView加载本地文件代码 下载PDF使用OKhttp ......

一、在对应模块的build.gradle文件中加入依赖

dependencies {

    implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'

}

二、activity布局xml文件中,加入com.github.barteksc.pdfviewer.pdfview控件

  

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".pdfactivity">

    <com.github.barteksc.pdfviewer.pdfview
        android:id="@+id/pdfview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.constraintlayout>

三、pdfview只能预览本地文件 如果是网络pdf还需要下载

  pdfview加载本地文件代码

/**
     * 查看pdf
     */
    private void seepdf(file dest) {
        try {
            pdfview.setvisibility(view.visible);

pdfview.usebestquality(false);
            constants.cache.cache_size=40;
            pdfview.fromfile(dest).load(); 
} catch (exception e) { e.printstacktrace(); } }

  下载pdf使用okhttp:

/**
     * 开始下载pdf
     */
    private void downloadpdf() {
        cacheurl = getcachedir().getabsolutepath();//应用缓存路径
        pdfname = mpdfurl.substring(mpdfurl.lastindexof("/") + 1);//文件名称
        final file dest = new file(cacheurl, pdfname);
        if (dest.exists()) {
            seepdf(dest);
        } else {
            request request = new request.builder().url(mpdfurl).build();
            new okhttpclient().newcall(request).enqueue(new callback() {
                @override
                public void onfailure(call call, ioexception e) {
                    // 下载失败
                }

                @override
                public void onresponse(call call, response response) throws ioexception {
                    sink sink = null;
                    bufferedsink bufferedsink = null;
                    try {
                        if (!dest.exists()) {
                            boolean newfile = dest.createnewfile();
                        }
                        sink = okio.sink(dest);
                        bufferedsink = okio.buffer(sink);
                        bufferedsink.writeall(response.body().source());
                        bufferedsink.close();
                        if (handler == null) {
                            handler = new handler(looper.getmainlooper());
                        }
                        handler.post(new runnable() {
                            @override
                            public void run() {
                                seepdf(dest);
                            }
                        });
                    } catch (exception e) {
                        e.printstacktrace();
                    } finally {
                        if (bufferedsink != null) {
                            bufferedsink.close();
                        }

                    }
                }
            });
        }
    }

  自动翻页的实现:

   1、在pdfview的onrenderlistener实现翻页,handler.postdelayed来定时执行翻页方法

    

 pdfview.fromfile(dest).onrender(new onrenderlistener() {
                @override
                public void oninitiallyrendered(int nbpages) {
                    if (pdf_trun_time != null) {
                        if (handler == null) {
                            handler = new handler();
                        }
                        handler.postdelayed(gonextpagerunnable, pdf_trun_time);
                    }
                }
            })
                    .load();


 private runnable gonextpagerunnable = new runnable() {
        @override
        public void run() {
            if (pdf_trun_time != null) {
                handler.postdelayed(this, pdf_trun_time);//设置循环时间,此处是5秒
                gonextpage();
            }
        }
    };



 private void gonextpage() {
        int totalpage = pdfview.getpagecount();
        int curpage = pdfview.getcurrentpage();
        int nextpage = 0;
        if (curpage < totalpage - 1) {
            nextpage = curpage + 1;
        } else {
            nextpage = 0;
        }

        pdfview.jumpto(nextpage, true);
    }