Android 实现通过url加载PDF

18 篇文章 0 订阅
13 篇文章 2 订阅

前两天用到PDF加载功能,需求是从url加载,本以为很简单的事,只需一个webview就解决了,没想到webview不支持,网上找了一些解决方案都不太理想,于是想自己封装一个。

开源的库基本没有支持url加载的(或者我没找到),我的实现思路是先把文件下载下来,再从已加载本地file的形式加载出来,开源库选择

barteksc/AndroidPdfViewer

传送门 点击打开链接
核心代码

   public void loadFromUrl(){
            final String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PDFViewCache/";
            int index = fileUrl.lastIndexOf("/");
            String fileName = fileUrl.substring(index);
            final File file = new File(SDPath, fileName);
            if(file.exists()){
                //文件存在
                if(onFileDownloadCompleteListener!=null){
                    onFileDownloadCompleteListener.onDownloadComplete(file);
                }
                PDFView.this.fromFile(file);
                load();
            }else{
                DownloadUtil.get().download(fileUrl, SDPath, new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        if(onFileDownloadCompleteListener!=null){
                            onFileDownloadCompleteListener.onDownloadComplete(file);
                        }
                        PDFView.this.fromFile(file);
                        load();
                    }

                    @Override
                    public void onDownloading(int progress) {

                    }

                    @Override
                    public void onDownloadFailed() {

                    }
                });
            }
        }
先判断文件是否已经缓存了,如果缓存了则直接加载,如果没缓存就下载文件并显示

使用方法

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.shxdos:AndroidPdfViewer:2.7.0-beta.2'
}
pdfView.fromUrl("http://www.anweitong.com/upload/document/standard/national_standards/138793918364316200.pdf")
                        .enableSwipe(true) // allows to block changing pages using swipe
                        .defaultPage(0)
                        .onLoad(this) // called after document is loaded and starts to be rendered
                        .onPageChange(this)
                        .swipeHorizontal(false)
                        .enableAntialiasing(true)
                        .onFileDownload(this)
                        .loadFromUrl();
github地址 https://github.com/shxdos/AndroidPdfViewer

源码下载 http://download.csdn.net/detail/shaohongxuan/9908649

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值