android 对pdf文件的下载、缓存、显示,包含android-pdfview框架使用教程
2017-09-05 00:00
155 查看
android-pdfview框架下载链接
一、下载android-pdfview框架
二、android studio项目集成android-pdfview
1、打开你需要集成的项目,在菜单选择file->New->Import Module
New->Import Module" title="">
2、选择android-pdfview
3、在您项目工程的build.gradle文件的 dependencies中添加一行代码
compile project(‘:android-pdfview’)
三、框架的使用
1、在布局文件中使用pdfview 标签
activity_pdf.xml
2、pdf的调用缓存、下载保存、显示pdf代码如下
PDFActivity.java
3、下载代码实现,本想教程采用xutils框架下载文件,框架使用方法建议网上搜索,相关实现代码如下
NetBase.java
DownloadCallBack.java
一、下载android-pdfview框架
二、android studio项目集成android-pdfview
1、打开你需要集成的项目,在菜单选择file->New->Import Module
New->Import Module" title="">
2、选择android-pdfview
3、在您项目工程的build.gradle文件的 dependencies中添加一行代码
compile project(‘:android-pdfview’)
三、框架的使用
1、在布局文件中使用pdfview 标签
activity_pdf.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.activity.PDFActivity"> <com.joanzapata.pdfview.PDFView android:id="@+id/pdfview" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
2、pdf的调用缓存、下载保存、显示pdf代码如下
PDFActivity.java
String path=getPath("app"); String fileName="file.pdf"; String url="http://www.qupu123.com/downpdf/505309/A%20Rock%20(Jazz%20Ensemble)1.pdf"; initFile(path,fileName,url); public String getPath(String filePath) { String path=""; if(null==filePath||"".equals(filePath)){ path=Environment.getExternalStorageDirectory().getPath() + "/"; }else { path = Environment.getExternalStorageDirectory().getPath() + "/" + filePath + "/"; } File file = new File(path); if (!file.exists()) { file.mkdir(); } return path; } private void initFile(String path,String fileName,String url){ File file=new File(path); if(!file.exists()){ file.mkdir(); } File file1=new File(path+fileName); if(!file1.exists()){ loadFile(url,path+fileName); }else{ showPDF(file1); } } private void loadFile(String url,String filePath){ NetBase netBase=new NetBase(mContext); netBase.downLoadFile(url, filePath, new DownloadCallBack() { @Override public void onStart(String url) { } @Override public void onLoading(Long current, Long total) { } @Override public void onSuccess(String url, String path) { File file2=new File(path); if(!file2.exists()){ ToastUtil.INSTANCE.showToast(mContext,"文件不存在"); return; } showPDF(file2); } @Override public void onFailure(String url) { } }); } private void showPDF(File file){ try { pdfview.fromFile(file) //.pages(0, 0, 0, 0, 0, 0) // 默认全部显示,pages属性可以过滤性显示 .defaultPage(1)//默认展示第一页 .onPageChange(this)//监听页面切换 .load(); }catch (Exception e){ } }
3、下载代码实现,本想教程采用xutils框架下载文件,框架使用方法建议网上搜索,相关实现代码如下
NetBase.java
public class NetBase { private Context mContext; private HttpUtils mHttpUtils; private BitmapUtils mBitmapUtils; public NetBase(Context context) { this.mContext = context; init(); } private void init() { XUtilsManager mXUtilsManager = XUtilsManager.getInstance(this.mContext); this.mHttpUtils = mXUtilsManager.getHttpUtils(); this.mBitmapUtils = mXUtilsManager.getBitmapUtils(); } public void downLoadFile(final String url, final String path, final DownloadCallBack callBack) { HttpUtils http = new HttpUtils(); try { HttpHandler handler = http.download(url, path, true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。 true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。 new RequestCallBack<File>() { @Override public void onStart() { callBack.onStart(url); } @Override public void onLoading(long total, long current, boolean isUploading) { } @Override public void onSuccess(ResponseInfo<File> responseInfo) { String filePath=responseInfo.result.getPath(); String newPath=path; File file=new File(filePath); file.renameTo(new File(newPath)); callBack.onSuccess(url, path); } @Override public void onFailure(HttpException error, String msg) { callBack.onFailure(url); } }); }catch (Exception e ){ e.toString(); } }
DownloadCallBack.java
public interface DownloadCallBack { void onStart(String url); void onLoading(Long current,Long total); void onSuccess(String url,String path); void onFailure(String url); }
相关文章推荐
- android 对pdf文件的下载、缓存、显示,包含android-pdfview框架使用教程
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- ios使用AFN框架下载文件显示下载百分比带进度条
- Android第三方框架universal-image-loader[图片缓存加载]及其OOM问题处理【常用】,CardView使用[给列表中项+边框]
- Android基础教程(一)之------更改与显示文字标签TextView标签的使用
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
- WebView使用(内存泄露+获取网页标题+js交互+调用浏览器下载文件+网页加载失败+清缓存)
- Fresco 使用教程(包括显示在线图片,资源图片、下载、清除缓存)
- Android使用缓存机制实现文件下载及异步请求图片加三级缓存
- Android基础教程(一)之------更改与显示文字标签TextView标签的使用
- Android 文件下载file-downloader框架的使用
- Android基础教程(一)之------更改与显示文字标签TextView标签的使用
- Android 在webview中下载pdf文件,并用自定义界面阅读
- [置顶] AndroidPdfView一行代码快速显示pdf,支持线上下载与显示
- Android下载文件显示到ImageView并保存在手机SD卡
- android开发教程教您用于下载文件和显示现在进度的线程类
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 3 - 按需载入视图(ViewStub的使用方法)]
- Android WebView使用实例(html5、文件下载和远程URL)
- android开源框架andorid-async-http使用(下载上传文件)