“如何在应用内的android webview中打开.ppt,.pptx,.doc,.xlsx文件?”
”“这是打开我已在sd卡中下载并打开witin android webview文件的要求。过去,我尝试了很多google建议的答案,它们是使用intent和MIME Type在应用程序外部打开文件或打开在线文件,例如webview.loadUrl(“ http://docs.google.com/gview?embedded=true&url=http://myurl.com/mySlide.ppt”);“
public class WebContentViewActivity extends BaseActivity {
@BindView(R.id.webView)
WebView webView;
private String docUrl="http://docs.google.com/gview?
embedded=true&url=";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder builder = new
StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
setContentView(R.layout.activity_video_view);
ButterKnife.bind(this);
WebSettings settings = webView.getSettings();
webView.clearCache(true);
webView.clearHistory();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebViewClient(new WebViewController(this));
filePath =
"/storage/emulated/0/New_Files/1550130536314somefile.pptx";
fileName = "1550130536314somefile.pptx";
if (!TextUtils.isEmpty(fileName )){
Uri fileUri = Uri.fromFile(fileName );
webView.loadUrl(fileUri.toString());
// webView.loadUrl(docUrl+fileUri);//getting 400.That's an error
on webview
}
/**
* Custom WebView controller class
*/
private class WebViewController extends WebViewClient {
private Context mContext;
//TODO : use this flag while open clicked url in the same WebView
public WebViewController(Context ctx) {
this.mContext = ctx;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
System.out.println("onPageStarted: " + url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.v("Webview Error", description);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
{
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
我想在代码中使用上述Webview显示任何文档文件。