第一种方案:
简单粗暴的采用第三方软件加载,也就是说不在当前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);
扫描二维码关注公众号,回复:
1842165 查看本文章
注意:最后一句限制了国内无法在线解析,无奈,友情提示:测试时候先搭建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面试宝典 -软件开发者必备