我必须在我的应用程序中显示远程URL链接中的不同类型的文档。我在这样的网络浏览中使用了Google Doc
private WebView mWvHome = (WebView) view.findViewById(R.id.wv_home);
mWvHome.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
}
});
mWvHome.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
}
});
mWvHome.getSettings().setJavaScriptEnabled(true); // enable javascript
//Pasting a Fixed URL link to the file I am getting this Error.
mWvHome.loadUrl("http://docs.google.com/gview?embedded=true&url=http://followitdc.cloudapp.net/FollowitMediaAPIv4/images/bb629006-d930-4fbe-b9ec-91895dc5133c.docx");
这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_normal"
android:layout_alignParentTop="true"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/custom_progressbar"
android:visibility="visible" />
<WebView
android:id="@+id/wv_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/progress" />
<RelativeLayout
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/progress"
android:background="@color/white">
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</RelativeLayout>
加载从谷歌文档获得此响应后。
.docx格式的文件大小小于2MB正在加载一些小于2MB的文件也没有加载。这是什么问题?是文件大小问题还是文件中的错误。因为文件在Google Doc在线编辑器和Microsoft Word中正常工作。任何帮助将不胜感激。
经过一番努力,我已经到达this link,他们提到“Office Web Apps Viewer”这是一个文件扩展名的在线应用程序,如('.ppt''.pptx''。doc','。docx','。xls', '.xlsx')您只需在网页浏览中提供在线文档链接即可
http://view.officeapps.live.com/op/view.aspx?src=[OFFICE_FILE_URL]
谷歌在线文档查看应用程序不能正确处理DOC和DOCX文件,并提出上述错误。但使用Office Live app进行办公文件扩展后,一切正常。
正如在WebView中所讨论的,在使用loadDataWithBaseURL
时,WebView可以访问本地设备文件(通过'文件'方案URL)
仅当baseUrl指定“http”,“https”,“ftp”,“ftps”,“about”或“javascript”以外的方案时。
但是,据我所知,WebView目前不支持显示office文档。如果您的目标只是将某些HTML显示为UI的一部分,那么这可能很好。
请检查此SO帖子中的解决方案 - Android how to open a .doc extention file和GitHub中的这篇文章 - AndroidDocxToHtml可能有所帮助。
用这个:
String fileUrl = "https://cs.wmich.edu/elise/courses/cs526/Android-tutorial.docx";
WebView personalWebView = findViewById(R.id.personalWebView);
personalWebView.getSettings().setJavaScriptEnabled(true);
personalWebView.loadUrl("https://drive.google.com/gview?embedded=true&url=" + fileUrl);
或这个:
String fileUrl = "https://cs.wmich.edu/elise/courses/cs526/Android-tutorial.docx";
String doc="<iframe src='http://docs.google.com/gview?embedded=true&url="+fileUrl+"' width='100%' height='100%' style='border: none;'></iframe>";
WebView personalWebView = findViewById(R.id.personalWebView);
personalWebView.getSettings().setJavaScriptEnabled(true);
personalWebView.loadData( doc , "text/html", "UTF-8");
只需添加
url=url.replaceAll(" ","%20");
String newUA= "Chrome/43.0.2357.65 ";
webView.getSettings().setUserAgentString(newUA);
webView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src="+url);
这里url是您的文件可用的变量。