Android平台下加载PDF文件方案解析
来源:互联网 发布:手机开淘宝店视频教程 编辑:程序博客网 时间:2024/06/06 04:24
第一种方案:
简单粗暴的采用第三方软件加载,也就是说不在当前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. 下载源码
前往 码农市场免费下载;
0 0
- Android平台下加载PDF文件方案解析
- Android平台下加载PDF文件方案解析
- Android pdf解析方案
- Android 加载PDF文件
- Android 加载PDF文件
- Android 加载PDF文件
- Android阅读PDF文件方案
- android 解析pdf 方案 使用mupdf
- android 解析pdf 方案 使用mupdf
- Android加载预览PDF文件
- android解析pdf文件muPdf
- android解析pdf文件muPdf
- android解析pdf文件muPdf
- android 平台加载obj文件
- (Android)不用下载,在线加载pdf文件
- cocos2dx android平台下 sqlite3 移植方案
- Android 平台解析CSV文件
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析
- 关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
- Qt Creator 你必须要掌握的快捷操作
- R语言针对txt文本的词云做法
- #!/usr/bin/env python与#!/usr/bin/python的区别
- iOS 【极光推送】第一次打开app,收不到推送消息
- Android平台下加载PDF文件方案解析
- HttpClient+Jsoup 抓取网页信息
- 训练营第二天作业
- python函数错误分析
- 定制特性 MISL表示
- RecyclerView自定义基础通用Adapter和ViewHolder
- STL 例题
- ubuntu17.04编译Tiny4412 Android5.0源代码
- 通过 PDFRenderer 实现预览PDF 功能