Android平台下加载PDF文件方案解析

第一种方案:

简单粗暴的采用第三方软件加载,也就是说不在当前APP内部打开,而是直接调用第三方APP去查阅PDF文件;

参考核心代码如下:

public void openPDFReader(int index) {
  if (dataList != null && dataList.size() > index) {
   CoverFlowData tempData = dataList.get(index);
//   Log.v("wenjianming>>>>>>>>>>", Global.magazinePath+tempData.name+".pdf");
         File file = new File(Global.magazinePath+tempData.name+".pdf");
         if (file.exists()) {
             Uri path = Uri.fromFile(file);
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(path, "application/pdf");
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             try {
                 startActivity(intent);
             } 
             catch (ActivityNotFoundException e) {
                 Toast.makeText(ActivityCoverFlow.this, 
                     "No Application Available to View PDF", 
                     Toast.LENGTH_SHORT).show();
             }
         }
  }



第二种方案:

先赞下iOS平台,加载PDF有各种方案,系统自带API,UIWebView无缝加载显示,可惜Android平台采用简单的WebView加载都是问题,首先因为历史原因,无法连接Google服务器来解析文件,导致方案变得非常被动;

参考核心代码如下:

 String pdfUrl = "http://www8.cao.go.jp/okinawa/8/2012/0409-1-1.pdf";  
 mWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+ pdfUrl);  


注意:最后一句限制了国内无法在线解析,无奈,友情提示:测试时候先搭建VPN环境;


第三种方案:

采用第三方开源库解析,推荐AndroidPdfViewer(github:https://github.com/barteksc/AndroidPdfViewer),它是基于Google的VuDroid类库来解码PDF文件,在APP内直接加载PDF文件,效果也不错;

参考核心代码如下:


1. 添加类库 build.gradle:

compile 'com.github.barteksc:android-pdf-viewer:1.4.0’

2. 布局文件

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent”/>


3. 加载代码

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromAsset(SAMPLE_FILE)
        .defaultPage(pageNumber)
        .onPageChange(this)
        .swipeVertical(true)
        .showMinimap(false)
        .enableAnnotationRendering(true)
        .onLoad(this)
        .load();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();


4. 下载源码

前往 码农市场免费下载;


推荐优质APP:IT面试宝典 -软件开发者必备


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群鸿

感谢认可,多谢打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值