Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to intercept and stop loading all mp4 videos on a webView , for that I'm using shouldInterceptRequest . I'm able to intercept the videos and, for ex:, show a dialog but, but I'm not able to stop loading them, any idea why?

webView.setWebViewClient(new WebViewClient() {
    @SuppressWarnings("deprecation")
    @Override
    public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
        try { 
            if (url.contains(".mp4?")) {
                return getResponseData();
        } catch (Exception e) {
            e.printStackTrace();
        return super.shouldInterceptRequest(view, url);
    private WebResourceResponse getResponseData() {
        try {
            String str = "Access Denied";
            InputStream data = new ByteArrayInputStream(str.getBytes("UTF-8"));
            return new WebResourceResponse("text/css", "UTF-8", data);
        } catch (IOException e) {
            return null;
                Are you certain that your getResponseData method is not returning null? That would cause the video the continue loading. Have you tried debugging the app using your IDE to see what exactly it's doing?
– NasaGeek
                Sep 23, 2015 at 17:46
                Put a log after if(url.contain(".mp4?)) . And why you have ? after .mp4 ? If the log wont show use if(url.endsWith(".mp4") .
– mehdok
                Sep 24, 2015 at 19:04
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    if (yourCondition) {
        Handler handler = new Handler(getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                YourwebView.stopLoading();
                YourwebView.doAnythingYouWant();
    return super.shouldInterceptRequest(view, request);

I created the thread because A WebView method ( that is stopLoading() ) can't be called on a currently running thread and that thread is 'Chrome_FileThread'.

Also, get getMainLooper() because In above situation we can't create handler inside thread that has not called Looper.prepare().

Hope this helps.

If your HTML page does not show mp4 contents or handles invalid content, then you can simply pass some dummy InputStream to WebResourceResponse as shown below.

my_webview.webViewClient = object : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        if (request?.url?.contains(".mp4") == true) {
            val inputStream = "".byteInputStream(Charset.defaultCharset())
            return WebResourceResponse("image/png", "UTF-8", inputStream)
        } else {
            return super.shouldInterceptRequest(view, request)
webView.setWebViewClient(new WebViewClient(){
  @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
               // code goes here

Try this

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (yourCondition) {
                    webView.post(new Runnable() {
                        @Override
                        public void run() {
                            webView.stopLoading();
                return null;
                Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
– Community
                Jun 7, 2022 at 5:25
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.