需求:页面中有一个地址为在线文档或者图片的附件,点击查看对应的附件详情
实现:使用原生的WebView
实现

具体实现代码
if (mUrl.endsWith(".png") || mUrl.endsWith(".jpg") || mUrl.endsWith(".jpeg")) {
mWebView.loadUrl(mUrl);
} else {
//通过在线预览office文档的地址加载
//微软:https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址); (有时不知道为什么访问不到服务器)
//谷歌:https://docs.google.com/viewer?url=(输入你的文档在服务器中的地址); ( 翻墙)
//xdoc: http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=(输入你的文档在服务器中的地址) (大小限制为4M)
//mWebView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src=" + mUrl);
mWebView.loadUrl("http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=" + mUrl);
}
图片是原生WebView就支持打开的,所以直接load,文档则需要使用第三方的浏览服务去加载,关于标题不对的问题,在打开WebViewActivity的时候知道标题的话可以带过去
修改(20210114)
今天发现.txt
读不出来,修改如下:
//打开office
if (mUrl.endsWith(".doc")
|| mUrl.endsWith(".docx")
|| mUrl.endsWith(".xls")
|| mUrl.endsWith(".xlsx")
|| mUrl.endsWith(".pdf")
|| mUrl.endsWith(".ppt")
|| mUrl.endsWith(".pptx")) {
//通过在线预览office文档的地址加载
//微软:https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址); (有时不知道为什么访问不到服务器)
//谷歌:https://docs.google.com/viewer?url=(输入你的文档在服务器中的地址); ( 翻墙)
//xdoc: http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=(输入你的文档在服务器中的地址) (大小限制为4M)
mWebView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src=" + mUrl);
//mWebView.loadUrl("http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=" + mUrl); (收费)
} else {
mWebView.loadUrl(mUrl);
}
错误修改(20210115)
20210114那天修改的,在加载线上PDF的时候加载失败,修改如下:
//打开office
if (mUrl.endsWith(".doc")
|| mUrl.endsWith(".docx")
|| mUrl.endsWith(".xls")
|| mUrl.endsWith(".xlsx")
|| mUrl.endsWith(".ppt")
|| mUrl.endsWith(".pptx")
) {
//通过在线预览office文档的地址加载
//微软:https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址); (有时不知道为什么访问不到服务器)
//谷歌:https://docs.google.com/viewer?url=(输入你的文档在服务器中的地址); ( 翻墙)
//谷歌:http://docs.google.com/gview?embedded=true&url=(输入你的文档在服务器中的地址); ( 翻墙)
//xdoc: http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=(输入你的文档在服务器中的地址) (大小限制为4M) (收费)
mWebView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src=" + mUrl);
//mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + mUrl);
//mWebView.loadUrl("http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=true&_xdoc=" + mUrl);
} else if (mUrl.contains(".pdf")) {
mWebView.loadUrl("file:///android_asset/pdf.html?" + mUrl);
} else {
mWebView.loadUrl(mUrl);
}
代码中加载PDF的方式,为此链https://www.jb51.net/article/136364.htm中的方式三。
bug,感觉加载依旧是很慢!
看到此文章的同仁,有好的希望推荐下,感谢!
网友评论