android 展示pdf文件 您所在的位置:网站首页 pdftron解析PDF内容 android 展示pdf文件

android 展示pdf文件

2024-07-04 18:34| 来源: 网络整理| 查看: 265

 注:此方式展示pdf文件会增加apk大小3-4m左右 建议使用腾讯浏览服务-x5进行加载pdf文件(可扩展)

X5内核初始化下载失败的解决方案

有道云笔记

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") 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); } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有