android 展示PDF文件

2,362 阅读1分钟
  •  注:此方式展示pdf文件会增加apk大小3-4m左右 建议使用x5的webview进行加载pdf文件(可扩展)

X5webview使用,最近也是非常不顺,一直初始化失败

不过鄙人已经解决了此问题,note.youdao.com/s/3FxTIJuA

1. 加入此依赖

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

2. 简单介绍

此篇文章主要还是将pdf文件进行下载到本sd目录下,之后转为file文件,交给pdfview进行展示,具体的展示pdf文件可进入pdfview源码中进行查看

https://github.com/barteksc/AndroidPdfViewer

3. 开始操作

public class PDF2Activity extends AppCompatActivity {
 
    private PDFView pdfView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_p_d_f2);
        pdfView = findViewById(R.id.pdfView);
 
        download("xxx.pdf");
 
    }
 
    private void download(String url) {
        DownloadUtil.download(url, getCacheDir() + "/temp.pdf", new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(final String path) {
                Log.d("MainActivity", "onDownloadSuccess: " + path);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        preView(path);
                    }
                });
            }
 
            @Override
            public void onDownloading(int progress) {
                Log.d("MainActivity", "onDownloading: " + progress);
            }
 
            @Override
            public void onDownloadFailed(String msg) {
                Log.d("MainActivity", "onDownloadFailed: " + msg);
            }
        });
    }
 
    private void preView(String path) {
        File file = new File(path);
        //这里只是作为一个file文件进行展示 还有其他的办法进行展示
        pdfView.fromFile(file)
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .defaultPage(0)
                // allows to draw something on the current page, usually visible in the middle of the screen
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .load();
    }

3.1 okhttp

implementation("com.squareup.okhttp3:okhttp:4.6.0")

4.DownLoadUtils

public class DownloadUtil {
 
    public static void download(final String url, final String saveFile, final OnDownloadListener listener) {
        Request request = new Request.Builder().url(url).build();
        new OkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                listener.onDownloadFailed(e.getMessage());
            }
 
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len;
                FileOutputStream fos = null;
                try {
                    is = response.body().byteStream();
                    long total = response.body().contentLength();
                    File file = new File(saveFile);
                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        listener.onDownloading(progress);
                    }
                    fos.flush();
                    listener.onDownloadSuccess(file.getAbsolutePath());
                } catch (Exception e) {
                    listener.onDownloadFailed(e.getMessage());
                } finally {
                    try {
                        if (is != null)
                            is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        if (fos != null)
                            fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
 
    public interface OnDownloadListener {
        void onDownloadSuccess(String path);
 
        void onDownloading(int progress);
 
        void onDownloadFailed(String msg);
    }
}