当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 如何本地加载pdf文件

Android 如何本地加载pdf文件

2019年07月24日  | 移动技术网移动技术  | 我要评论

大部分app打开pdf文件是通过intent调起手机中能打开pdf文件的工具,来查看pdf文件,如果需求是,用户在app内下载好pdf文件后,不通过第三方的工具,本地打开。

这样的需求要怎么实现呢?上网查了一些资料,发现了一个很好用pdf开源库。

使用起来也很简单,首先添加pdfview的引用

compile 'com.github.barteksc:android-pdf-viewer:2.4.0'

布局中引用pdfview

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <include layout="@layout/common_title" />

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

接下来就是下载pdf文件,为了节省用户资源,在每次下载之前检查一下本地是否有该pdf文件,如果有直接打开,没有的话再去下载。

这里我写了一个加载中的对话框,打开过程中和下载过程中用的都是这一个

if (checkfileexist(title)){
   buildershow = new customdialog(showpdfactivity.this);
   layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service);
   view view = inflater.inflate(r.layout.dialog_pdf_progress_new, null);
   buildershow.setcontentview(view);
   buildershow.show();
   isdownload=false;
   refushui();
  }else {
   isdownload=true;
   downloadpdf.getinstance().downloadpdf(showpdfactivity.this, //下载路径);

  }

如果本地有pdf文件,则开始加载pdf文件,refushui();

public void refushui(){
  try {
   pdfview.fromfile(new file(//pdf文件的绝对路径,//标题))
     .defaultpage(1)
     .enableannotationrendering(false)
     .onload(new onloadcompletelistener() {
      @override
      public void loadcomplete(int nbpages) {
       if (isdownload){
        downloadpdf.getinstance().closedilaoig();
       }
       if (buildershow != null&&buildershow.isshowing()) {
        buildershow.dismiss();
       }
      }
     })
     .scrollhandle(null)
     .load();
  }catch (exception e){
   e.printstacktrace();
  }
 }

pdfview加载pdf文件有两种形式,一种是从文件中读取,还有一种就是从assets目录中读取

private void displayfromassets(string assetfilename ) {
  pdfview.fromasset(assetfilename) //设置pdf文件地址
    .defaultpage(6)   //设置默认显示第1页
    .onpagechange(this)  //设置翻页监听
    .onload(this)   //设置加载监听
    .ondraw(this)   //绘图监听
    .showminimap(false)  //pdf放大的时候,是否在屏幕的右上角生成小地图
    .swipevertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
    .enableswipe(true) //是否允许翻页,默认是允许翻页
    // .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
    .load();
 }

 private void displayfromfile( file file ) {
  pdfview.fromfile(file) //设置pdf文件地址
    .defaultpage(6)   //设置默认显示第1页
    .onpagechange(this)  //设置翻页监听
    .onload(this)   //设置加载监听
    .ondraw(this)   //绘图监听
    .showminimap(false)  //pdf放大的时候,是否在屏幕的右上角生成小地图
    .swipevertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
    .enableswipe(true) //是否允许翻页,默认是允许翻
    // .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
    .load();
 }

本地没有pdf文件,需要从服务端获取,

 downloadpdf.getinstance().downloadpdf(showpdfactivity.this, //下载路径);

public class downloadpdf {
 private static context context;
 private static file file ;
 private static customdialog builder = null ;
 private static handler ddhandle;
 private static downloadpdf instance = null;
 public static downloadpdf getinstance(){
  if(instance==null){
   synchronized (downloadpdf.class){
    if(instance==null){
     instance = new downloadpdf();
    }
   }
  }
  return instance;
 }
 public void downloadpdf(final context con, final string url, final string title, final handler ddhandler) {
  ddhandle = ddhandler;
  context = con;
  builder = new customdialog(con);
  layoutinflater inflater = (layoutinflater) con.getsystemservice(context.layout_inflater_service);
  view view = inflater.inflate(r.layout.dialog_pdf_progress_new, null);
  builder.setcontentview(view);
  builder.show();

  new thread() {
   @override
   public void run() {
    try {
     file = getfilefromserver(url,title);
     sleep(200);
     if (file != null) {
      handler.sendemptymessage(2);
     }
    } catch (exception e) {
     e.printstacktrace();
     builder.dismiss();
     handler.sendemptymessage(-1);
    }
   }
  }.start();
 }
 public void closedilaoig(){
  if (builder != null&&builder.isshowing()) {
   builder.dismiss();
  }
 }public static int length ;
 public static file getfilefromserver(string path,string title)
   throws exception {
  // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
  if (environment.getexternalstoragestate().equals(
    environment.media_mounted)) {
   url url = new url(path);
   httpurlconnection conn = (httpurlconnection) url.openconnection();
   conn.setconnecttimeout(5000);
   conn.setdoinput(true);
   conn.connect();
   length = conn.getcontentlength();
   inputstream is = conn.getinputstream();
   //将pdf文件存储在指定文件夹下
   file filepath = new file(//指定文件夹路径);
   if (!filepath.exists()){
    filepath.mkdir();
   }
   file file = new file(filepath , title+".pdf");
   fileoutputstream fos = new fileoutputstream(file);
   bufferedinputstream bis = new bufferedinputstream(is);
   byte[] buffer = new byte[1024];
   int len;
   while ((len = bis.read(buffer)) != -1) {
    fos.write(buffer, 0, len);
    handler.sendemptymessage(0);
   }
   fos.close();
   bis.close();
   is.close();
   return file;
  } else {
   handler.sendemptymessage(-1);
   return null;
  }
 }
 private static handler handler = new handler(){
  @override
  public void handlemessage(message msg) {
   super.handlemessage(msg);
   switch (msg.what) {
   case 0:
    break;
   case -1:
    //下载失败
    toast.maketext(context, "下载失败,请稍后再试!", toast.length_short).show();
    break;
   case 2:
    ddhandle.sendemptymessage(100);
    break;
   default:
    break;
   }
  }

 };
}

大家可以看到,在pdf问价下载成功的时候handler.sendemptymessage(2);,当case为2的时候,通过调用该工具类的页面传过来的ddhandle重新发送了一个消息,

调用界面收到消息后会重新调用refushui();这个方法来打开pdf文件。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网