android 对pdf文件的下载、缓存、显示,包含android-pdfview框架使用教程

来源:互联网 发布:交淘宝保证金在哪里交 编辑:程序博客网 时间:2024/06/08 00:50

android-pdfview框架下载链接

一、下载android-pdfview框架
二、android studio项目集成android-pdfview
1、打开你需要集成的项目,在菜单选择file->New->Import Module
选择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<File>() {                        @Override                        public void onStart() {                            callBack.onStart(url);                        }                        @Override                        public void onLoading(long total, long current,                                              boolean isUploading) {                        }                        @Override                        public void onSuccess(ResponseInfo<File> 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);}
原创粉丝点击