Android 如何本地加载pdf文件
大部分app打开pdf文件是通过intent调起手机中能打开pdf文件的工具,来查看pdf文件,如果需求是,用户在app内下载好pdf文件后,不通过第三方的工具,本地打开。
这样的需求要怎么实现呢?上网查了一些资料,发现了一个很好用PDF开源库。
使用起来也很简单,首先添加PDFView的引用
compile 'com.github.barteksc:android-pdf-viewer:2.4.0'
布局中引用PdfView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <include layout="@layout/common_title" /> <com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdf_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
接下来就是下载pdf文件,为了节省用户资源,在每次下载之前检查一下本地是否有该pdf文件,如果有直接打开,没有的话再去下载。
这里我写了一个加载中的对话框,打开过程中和下载过程中用的都是这一个
if (CheckFileExist(title)){
builderShow = new CustomDialog(ShowPDFActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null);
builderShow.setContentView(view);
builderShow.show();
isDownload=false;
refushUI();
}else {
isDownload=true;
DownLoadPDF.getInstance().downLoadPDF(ShowPDFActivity.this, //下载路径);
}
如果本地有pdf文件,则开始加载pdf文件,refushUI();
public void refushUI(){
try {
pdfView.fromFile(new File(//pdf文件的绝对路径,//标题))
.defaultPage()
.enableAnnotationRendering(false)
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
if (isDownload){
DownLoadPDF.getInstance().closeDilaoig();
}
if (builderShow != null&&builderShow.isShowing()) {
builderShow.dismiss();
}
}
})
.scrollHandle(null)
.load();
}catch (Exception e){
e.printStackTrace();
}
}
PDFView加载pdf文件有两种形式,一种是从文件中读取,还有一种就是从assets目录中读取
private void displayFromAssets(String assetFileName ) {
pdfView.fromAsset(assetFileName) //设置pdf文件地址
.defaultPage() //设置默认显示第1页
.onPageChange(this) //设置翻页监听
.onLoad(this) //设置加载监听
.onDraw(this) //绘图监听
.showMinimap(false) //pdf放大的时候,是否在屏幕的右上角生成小地图
.swipeVertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.enableSwipe(true) //是否允许翻页,默认是允许翻页
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
.load();
}
private void displayFromFile( File file ) {
pdfView.fromFile(file) //设置pdf文件地址
.defaultPage() //设置默认显示第1页
.onPageChange(this) //设置翻页监听
.onLoad(this) //设置加载监听
.onDraw(this) //绘图监听
.showMinimap(false) //pdf放大的时候,是否在屏幕的右上角生成小地图
.swipeVertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.enableSwipe(true) //是否允许翻页,默认是允许翻
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
.load();
}
本地没有pdf文件,需要从服务端获取, DownLoadPDF.getInstance().downLoadPDF(ShowPDFActivity.this, //下载路径);
public class DownLoadPDF {
private static Context context;
private static File file ;
private static CustomDialog builder = null ;
private static Handler ddhandle;
private static DownLoadPDF instance = null;
public static DownLoadPDF getInstance(){
if(instance==null){
synchronized (DownLoadPDF.class){
if(instance==null){
instance = new DownLoadPDF();
}
}
}
return instance;
}
public void downLoadPDF(final Context con, final String url, final String title, final Handler ddhandler) {
ddhandle = ddhandler;
context = con;
builder = new CustomDialog(con);
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null);
builder.setContentView(view);
builder.show();
new Thread() {
@Override
public void run() {
try {
file = getFileFromServer(url,title);
sleep();
if (file != null) {
handler.sendEmptyMessage();
}
} catch (Exception e) {
e.printStackTrace();
builder.dismiss();
handler.sendEmptyMessage(-);
}
}
}.start();
}
public void closeDilaoig(){
if (builder != null&&builder.isShowing()) {
builder.dismiss();
}
}public static int length ;
public static File getFileFromServer(String path,String title)
throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout();
conn.setDoInput(true);
conn.connect();
length = conn.getContentLength();
InputStream is = conn.getInputStream();
//将pdf文件存储在指定文件夹下
File filePath = new File(//指定文件夹路径);
if (!filePath.exists()){
filePath.mkdir();
}
File file = new File(filePath , title+".pdf");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[];
int len;
while ((len = bis.read(buffer)) != -) {
fos.write(buffer, , len);
handler.sendEmptyMessage();
}
fos.close();
bis.close();
is.close();
return file;
} else {
handler.sendEmptyMessage(-);
return null;
}
}
private static Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case :
break;
case -:
//下载失败
Toast.makeText(context, "下载失败,请稍后再试!", Toast.LENGTH_SHORT).show();
break;
case :
ddhandle.sendEmptyMessage();
break;
default:
break;
}
}
};
}
大家可以看到,在pdf问价下载成功的时候handler.sendEmptyMessage(2);,当case为2的时候,通过调用该工具类的页面传过来的ddhandle重新发送了一个消息,
调用界面收到消息后会重新调用refushUI();这个方法来打开pdf文件。
以上就是我对本地加载pdf文件方法的总结,如果大家在使用的过程中有不理解或错误的地方,欢迎骚扰!
Android 如何本地加载pdf文件的更多相关文章
- cordova程序加载pdf文件的2种方法(ios/android)
前言 公司目前的前端架构是微信端由vue全家桶负责h5网站的单页应用,android端和ios端则选择cordova打包成apk和app.其中,有一个业务逻辑是点击某个链接进入pdf的展示,h5的方案 ...
- Android studio 使用心得(六)—android studio 如何加载.so文件
之前一直没怎么注意,以为.so文件android为像eclipse一样直接加载,但是直到昨天我在android studio上调试公司项目推送消息的时候,才发现,.so文件原来没有加载成功. 可能之前 ...
- 使用 pdf.js 在网页中加载 pdf 文件
在网页中加载并显示PDF文件是最常见的业务需求.例如以下应用场景:(1)在电商网站上购物之后,下载电子发票之前先预览发票.(2)电子商务管理系统中查看发布的公文,公文文件一般是PDF格式的文件. 目前 ...
- android使用webview加载flash文件
android 字段webview几乎实现了浏览器的全部功能,最近在使用webview加载不固定格式的文章,文章中有一部分嵌入了flash,下面就是webview可以进行视频需要进行的设置,代码如下: ...
- IOS加载PDF文件
今天的任务是:在iOS上加载显示pdf文件. 方法一:利用webview -(void)loadDocument:(NSString *)documentName inView:(UIWebView ...
- iOS 本地加载js文件
#import "RootViewController.h" @interface RootViewController ()<UIWebViewDelegate> @ ...
- 【Android Studio】 加载so文件异常
AS无法加载so包异常 android studio导入so包异常:Couldn't load DeviceAPI from loader dalvik.system.PathClassLoader[ ...
- .net使用pdfobject.js加载pdf文件
1.下载pdfobject.js文件 2. <script type="text/javascript" src="<%= Application[" ...
- JavaEE SSH集成框架(两) struts2 本地加载dtd文件,action组态
1. 载入中struts2的dtd文件.使struts.xml网络无法验证,和eclipse有技巧 在src在创建struts.xml: <? xmlversion="1.0" ...
随机推荐
- 1782: [Usaco2010 Feb]slowdown 慢慢游
1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 570 Solved: 346[Sub ...
- PHP数据访问增删查(20161028)
注:预定义数组 $_POST[ ]; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- Python模块的动态加载机制
Python在运行环境初始化中,就将sys module加载到了内存中, 实际上,Python是将一大批的module加载到了内存中.但是为了使local名字空间能够达到最干净的效果,Python并没 ...
- 《连载 | 物联网框架ServerSuperIO教程》- 16.OPC Server的使用步骤。附:3.3 发布与版本更新说明。
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- LBPL--基于Asp.net、 quartz.net 快速开发定时服务的插件化项目
LBPL 这一个基于Asp.net. quartz.net 快速开发定时服务的插件化项目 由于在实际项目开发中需要做定时服务的操作,大体上可以理解为:需要动态化监控定时任务的调度系统. 为了实现快速开 ...
- IOS百度地图之--->第二篇《大头针__简单使用及自定义》
呵呵!大家不要只看帖不回帖么,要不然我都没有积极性了. 第一步:创建一个用来呈现mapview的viewcontroller,不废话直接贴代码 BasicMapViewControlle ...
- 《Django By Example》第十章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译本章过程中几次想放弃,但是既然 ...
- pyqt样式表语法笔记(上) --原创
pyqt样式表语法笔记(上) pyqt QSS python 样式表 因为软件课设的原因开始学习使用pyqt4,才发现原来它也有样式表,而且语法跟css基本相同,而且一些功能实现起来感觉比js要简单方 ...
- PL/SQL编程重点语句输出整理
create or replace procedure pr_mytest is v_test number() :=; v_char varchar2():='数据库'; c_changl cons ...
- oracle查询锁表解锁语句
--oracle查询锁表解锁语句--首先要用dba权限的用户登录,建议用system,然后直接看sql吧 --1. 如下语句 查询锁定的表: SELECT l.session_id sid, s.se ...