您的位置:首页 > 移动开发 > Android开发

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

<?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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐