1 RequireMent
加载网络pdf文件: http://file.chmsp.com.cn/colligate/file/00100000224821.pdf
2 scheme
(1) webView加载 ,谷歌服务有限制
(2) 跳转浏览器,体验差
(3) 下载后调用手机内APP,体验差
(4) 三方依赖库
3 Core Code
(1) webView加载
//找不到网页
private void webViewLoadPdf() {
//wv
WebView wb = findViewById(R.id.wv);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.loadUrl("https://docs.google.com/viewer?url=http://file.chmsp.com.cn/colligate/file/00100000224821.pdf");
}
(2)跳转浏览器
//体验差 跳转浏览器,两次跳转(浏览器+ 点击下载后的通知跳转能够打开pdf的app)
private void skipBrowser(){
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://file.chmsp.com.cn/colligate/file/00100000224821.pdf");
intent.setData(content_url);
startActivity(intent);
}
(3)下载后调用app
private void downLoadAndSkip(){
new AppFileDownUtils("http://file.chmsp.com.cn/colligate/file/00100000224821.pdf", "00100000224821.pdf", new DownLoadListener() {
@Override
public void onDownLoadSuccess(File file) {
startActivity(getPdfFileIntent(file));
}
@Override
public void onDownLoadFailed(String msg) {
}
@Override
public void onDownLoading(int progress) {
Log.i("onDownLoading",">>> progress ="+progress);
}
}).start();
}
public Intent getPdfFileIntent(File file) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
return Intent.createChooser(intent, "Open File");
}
(4)三方依赖库:PDFView
implementation 'com.github.barteksc:android-pdf-viewer:2.7.0-beta.1'
pdfView.fromFile(file)
// .pages(0, 2, 3, 4, 5); // 把0 , 2 , 3 , 4 , 5 过滤掉
//是否允许翻页,默认是允许翻页
.enableSwipe(true)
//pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.swipeHorizontal(true)
//
.enableDoubletap(false)
//设置默认显示第0页
.defaultPage(0)
//允许在当前页面上绘制一些内容,通常在屏幕中间可见。
// .onDraw(onDrawListener)
// // 允许在每一页上单独绘制一个页面。只调用可见页面
// .onDrawAll(onDrawListener)
//设置加载监听
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
pageTv.setText(nbPages + "");
pageTv1.setText(0 + "/");
}
})
//设置翻页监听
.onPageChange(new OnPageChangeListener() {
@Override
public void onPageChanged(int page, int pageCount) {
p = page;
pageTv1.setText(page + "/");
}
})
//设置页面滑动监听
// .onPageScroll(onPageScrollListener)
// .onError(onErrorListener)
// 首次提交文档后调用。
// .onRender(onRenderListener)
// 渲染风格(就像注释,颜色或表单)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
// 改善低分辨率屏幕上的渲染
.enableAntialiasing(true)
// 页面间的间距。定义间距颜色,设置背景视图
.spacing(0)
.load();