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

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

转载于:https://my.oschina.net/nandecanghai123/blog/1529342

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值