4

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

Pic1.What i want Pic2.What is happing (The Issue)

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

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
  • 1
    did you solve your problem? i am also doing exactly same thing but unsuccessful till now...if you have a sol please share – Muhammad Natiq Sep 22 '17 at 16:42
  • You need to customize the javascript in the webview and get that click event using javascript interface in android.Check my other question's answer https://stackoverflow.com/questions/40586193/get-video-url-from-facebook-in-webview – Burhanuddin Rashid Sep 22 '17 at 17:48
  • did you found any solution for this? @BurhanuddinRashid – User Apr 15 '21 at 06:06

0 Answers0