Android 加載PDF文件

今天按項目要求找了一個android的PDF控件,各種操作效果都非常好,在這里和大家分享一下。

用法很簡單:

1.在build.gradle里面添加依賴?compile 'com.github.barteksc:android-pdf-viewer:2.8.0'

2.然后就可以直接在layout里面使用了

3.在Activity里面引用資源,就可以打開pdf文件了,而且這個控件有超級多的方法,支持很多功能

? ? ? ?pdfView= findViewById(R.id.pdfview);

? ? ? String url="http://www8.cao.go.jp/okinawa/8/2012/0409-1-1.pdf";

? ? ? String name=url.split("/")[url.split("/").length-1];

? ? ? String pdfPath =Environment.getExternalStorageDirectory().getAbsolutePath() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +"/Android/data/com.demo/downloadPPT/"+name; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

? ? ? File file =newFile(pdfPath);

? ? ? ?if(file.exists()){ //如果文件存在直接打開??

? ? ? ? ? ? Uri uri = Uri.fromFile(file);

? ? ? ? ? ? ?pdfView.fromUri(uri)? ? ? ? ??

? ? ? ? ? ? ? ? ?.defaultPage(1)? ? ? ? ? ?

? ? ? ? ? ? ? ? .enableSwipe(true)? ? ? ??

? ? ? ? ? ? ? ? ?.load();

? ? ?}else{? ? ?

? ? ? ? ? ? ? showZpDialog("加載中...",2);

? ? ? ? ? ? ? downLoadFile(url,pdfPath);//下載PDF文件

? ? }

private void ?downLoadFile(String url, finalString pdfPath) { ? ? ? ? ? ? ? ? ? ? ?FileDownloader.getImpl().create(url).setWifiRequired(true).

setPath(pdfPath).setListener(newFileDownloadListener() {

@Override

protected void pending(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }

@Override

protected void ?progress(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }

@Override

protected void blockComplete(BaseDownloadTask task) {? ? ? ? }

@Override

protected void completed(BaseDownloadTask task) {? ? ? ? ??

? ? ? ?hideZpDialog();//隱藏加載框

? ? ? ? ?Toast.makeText(LookPPTActivity.this,"下載完成!",Toast.LENGTH_SHORT).show();

? ? ? ? ?File file =newFile(pdfPath);

? ? ? ? ?Uri uri = Uri.fromFile(file);

? ? ? ? ? ? ?pdfView.fromUri(uri)? ? ? ? ? ? ? ? ? ?

?.defaultPage(1)? ? ? ? ? ? ? ? ? ?

?.enableSwipe(true)? ? ? ? ? ? ? ? ? ??

.load();

}

@Override

protected void paused(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }

@Override

protected void error(BaseDownloadTask task,Throwable e) {? ? ? ? ? ?

? ? ? ? hideZpDialog();

}

@Override?

protected void warn(BaseDownloadTask task) {}??

? }).start();

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容