android-pdfview框架下载链接

一、下载android-pdfview框架
二、android studio项目集成android-pdfview
1、打开你需要集成的项目,在菜单选择file->New->Import Module

2、选择android-pdfview

3、在您项目工程的build.gradle文件的 dependencies中添加一行代码
compile project(‘:android-pdfview’)
三、框架的使用
1、在布局文件中使用pdfview 标签

activity_pdf.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    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"    android:orientation="vertical"    tools:context="com.example.activity.PDFActivity"><com.joanzapata.pdfview.PDFView    android:id="@+id/pdfview"    android:layout_width="match_parent"    android:layout_height="match_parent"/>LinearLayout>

2、pdf的调用缓存、下载保存、显示pdf代码如下
PDFActivity.java

String path=getPath("app");String fileName="file.pdf";String url="http://www.qupu123.com/downpdf/505309/A%20Rock%20(Jazz%20Ensemble)1.pdf";initFile(path,fileName,url);public String getPath(String filePath) {        String path="";        if(null==filePath||"".equals(filePath)){          path=Environment.getExternalStorageDirectory().getPath() + "/";        }else {            path = Environment.getExternalStorageDirectory().getPath() + "/" + filePath + "/";        }        File file = new File(path);        if (!file.exists()) {            file.mkdir();        }        return path;    }private void initFile(String path,String fileName,String url){        File file=new File(path);        if(!file.exists()){            file.mkdir();        }        File file1=new File(path+fileName);        if(!file1.exists()){            loadFile(url,path+fileName);        }else{            showPDF(file1);        }    }    private void loadFile(String url,String filePath){        NetBase netBase=new NetBase(mContext);        netBase.downLoadFile(url, filePath, new DownloadCallBack() {            @Override            public void onStart(String url) {            }            @Override            public void onLoading(Long current, Long total) {            }            @Override            public void onSuccess(String url, String path) {                File file2=new File(path);                if(!file2.exists()){                    ToastUtil.INSTANCE.showToast(mContext,"文件不存在");                    return;                }                showPDF(file2);            }            @Override            public void onFailure(String url) {            }        });    }    private void showPDF(File file){        try {            pdfview.fromFile(file)                    //.pages(0, 0, 0, 0, 0, 0) // 默认全部显示,pages属性可以过滤性显示                    .defaultPage(1)//默认展示第一页                    .onPageChange(this)//监听页面切换                    .load();        }catch (Exception e){        }    }

3、下载代码实现,本想教程采用xutils框架下载文件,框架使用方法建议网上搜索,相关实现代码如下

NetBase.java

public class NetBase {    private Context mContext;    private HttpUtils mHttpUtils;    private BitmapUtils mBitmapUtils;    public NetBase(Context context) {        this.mContext = context;        init();    }    private void init() {        XUtilsManager mXUtilsManager = XUtilsManager.getInstance(this.mContext);        this.mHttpUtils = mXUtilsManager.getHttpUtils();        this.mBitmapUtils = mXUtilsManager.getBitmapUtils();    }    public  void downLoadFile(final String url, final String path,  final DownloadCallBack callBack) {        HttpUtils http = new HttpUtils();        try {            HttpHandler handler = http.download(url, path, true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。                    true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。                    new RequestCallBack() {                        @Override                        public void onStart() {                            callBack.onStart(url);                        }                        @Override                        public void onLoading(long total, long current,                                              boolean isUploading) {                        }                        @Override                        public void onSuccess(ResponseInfo responseInfo) {                            String filePath=responseInfo.result.getPath();                            String newPath=path;                            File file=new File(filePath);                            file.renameTo(new File(newPath));                            callBack.onSuccess(url, path);                        }                        @Override                        public void onFailure(HttpException error, String msg) {                            callBack.onFailure(url);                        }                    });        }catch (Exception e ){            e.toString();        }    }

DownloadCallBack.java

public interface DownloadCallBack {        void onStart(String url);        void onLoading(Long current,Long total);        void onSuccess(String url,String path);        void onFailure(String url);}

更多相关文章

  1. Android多级文件夹建立
  2. android环境配置-again-遇到的问题总结
  3. Android环境搭建自己的总结~~(Windows下的)
  4. 【Android(安卓)P】 JobScheduler服务源码解析(二) ——框架解析
  5. 第二课 - Android项目目录结构
  6. Android开发常用调试技术大全
  7. Android(安卓)开源库——侧滑菜单栏(SlidingMenu)的导入和使用
  8. Android——使用ContentProvider在应用间传递数据
  9. Android中的多线程机制

随机推荐

  1. css详解position五种属性用法及其含义
  2. 深入了解JavaScript中基于原型(prototype)
  3. 一文学会怎样设置AppleWatch手表自动解锁
  4. css详解background八大属性及其含义
  5. 一篇文章带你初步了解—CSS特指度
  6. JavaScript遍历对象方法总结
  7. 有趣的css—隐藏元素的7种思路
  8. PHP:变量的类型转换方式,变量值传递与值引
  9. 210425 PHP入门
  10. 如何用nodeJs向别的服务器上传文件发送fo