Last active
February 13, 2023 03:27
-
-
Save kibotu/32313b957cd01258cf67 to your computer and use it in GitHub Desktop.
Android WebView: resource interception and replacement by local resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mWebView.setWebViewClient(new WebViewClient() { | |
@Override | |
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { | |
if (url.contains("creditcard_cvc.jpg")) { | |
Log.v("WebView", "Replacing [" + url + "] with [R.raw.tmp_replacement]"); | |
ContentResolver contentResolver = getActivity().getContentResolver(); | |
return new WebResourceResponse(contentResolver.getType(Uri.parse(url)), "UTF-8", getResources().openRawResource(R.raw.tmp_replacement)); | |
} | |
return super.shouldInterceptRequest(view, url); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { | |
if (request.getUrl().getEncodedPath().contains("creditcard_cvc.jpg")) { | |
Log.v("WebView", "Replacing [" + request.getUrl() + "] with [R.raw.tmp_replacement]"); | |
ContentResolver contentResolver = getActivity().getContentResolver(); | |
return new WebResourceResponse(contentResolver.getType(request.getUrl()), "UTF-8", getResources().openRawResource(R.raw.tmp_replacement)); | |
} | |
return super.shouldInterceptRequest(view, request); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This only works for Image type(jpg, or png ...), it doesn't work for video like mp4.