之前做法是偷偷下载文档调用第三方wps打开,完事后偷偷删除,说实话这样有点难受。
后来发现浏览器使用worldonline可以查看文档,放到Android自带webview上直接是一个小红叉,
点击链接直接飞到微软官网(这个方法凉凉),之前使用AgentWebview做Vue交互感觉很强大,
放到Android上在线查看文档居然可以,最后考虑使用AgentWebview加载微软在线查看工具
亲,这边建议您使用AgentWebView来加载哦!(向电商大佬看齐)
具体方法:
(普通自带webview不能使用)调用worldonline地址: http://view.officeapps.live.com/op/view.aspx?src=
AgentWebview播放界面
public class AgentWebActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agent_web);
//online_officelinear作为Agentwebview依附的控件
LinearLayout online_officelinear = findViewById(R.id.online_officelinear);
NestedScrollAgentWebView webView = new NestedScrollAgentWebView(this);
AgentWeb.with(this)
.setAgentWebParent(online_officelinear, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ,如果父控件为 RelativeLayout , 那么第二参数需要传入 RelativeLayout.LayoutParams
.useDefaultIndicator()
.setWebView(webView)// 使用默认进度条
.createAgentWeb()//
.ready()
.go("http://view.officeapps.live.com/op/view.aspx?src=xxxxxx.docx");//XXX替换为在线查看链接
}
}
使用时 worldonline地址+在线文件的地址
大佬的AgentWebview地址: https://github.com/Justson/AgentWeb
AgentWebview使用:
导入依赖或者把AgentWebView作为module导入工程
使用AgentWebview需要新建一个AgentWebActivity作为打开webview的界面
AgentWebActivity的布局:
注意xxxxxxxxx,别直接复制,只需新建布局即可
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="xxxxxxxxx">
<LinearLayout
android:id="@+id/online_officelinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"></LinearLayout>
</android.support.constraint.ConstraintLayout>
效果图:

客官觉得有用的点个赞
demo需要自行替换在线文档地址,使用查看doc文件链接可能需要修改后缀为大写,如出现小红叉请检查文件格式或者放到浏览器看是否能够下载
demo地址:https://github.com/sweet-guy/RecycleViewOfType
还有一种方法使用wps打开在线或者本地文件(本地必须先下载好wps)
// 专用wps工具打开word,excel,ppt
public void openFile(String path) {
//判断是否下载wps
if (!this.isAvailable(context, "cn.wps.moffice_eng")) {
try {
this.AlertInstallWPS(context, "market://details?id=cn.wps.moffice_eng");
} catch (Exception var6) {
var6.printStackTrace();
}
}else{
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setClassName("cn.wps.moffice_eng", "cn.wps.moffice.documentmanager.PreStartActivity");
Uri uri = null;
if (this.isLocal(filePath)) {
uri = Uri.fromFile(new File(filePath));
} else {
uri = Uri.parse(filePath);
}
intent.setData(uri);
startActivity(intent);
}
//判断是否本地文件
private Boolean isLocal(String filePath) {
return filePath.startsWith("http") ? false : true;
} |