I want to download video from Facebook where i am loading Facebook in Webview
.
This is how i setup my Webview
in fragment:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
webView = new WebView(getActivity());
webView.getSettings().setJavaScriptEnabled(true);
webView.setInitialScale(50);
webView.getSettings().setUseWideViewPort(true);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.loadUrl("http://m.facebook.com");
webView.setWebViewClient(new CustomWebViewClient());
return webView;
}
So in order to download video from browser i am intercepting it all request and checking weather it link contains mp4 or not by CustomWebViewClient:
CustomWebViewClinet :
public class CustomWebViewClient extends WebViewClient {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, final String url) {
if (url.contains("mp4")) {
Log.e(TAG, "shouldInterceptRequest: got mp4\n" + url);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showToast("Got mp4");
//Download Logic Here with "url"
}
});
return null;
}
return super.shouldInterceptRequest(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
Now problem is whenever i clicked on video i got my mp4 url in shouldInterceptRequest()
but in Webview
it start playing the video in its Webview
player which i don't want.I want to prevent playing video in Webview
.I just want to download video on just clicking video without playing it
As you can see in Pic1 is does not play video in webview when it is clicked it show dialog to download but in my case in Pic2 its start playing video in webview when dialog is poped