-
-
Save 408881465/b56f7b508af9884a3c9148815c059531 to your computer and use it in GitHub Desktop.
Sample code of WebResourceResponse to load WebView resources from local assets!
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
@Override | |
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { | |
String resourceUrl = request.getUrl().toString(); | |
String fileExtension = WebviewResourceMappingHelper.getInstance().getFileExt(resourceUrl); | |
if(WebviewResourceMappingHelper.getInstance().getOverridableExtensions().contains(fileExtension)){ | |
String encoding = "UTF-8"; | |
String assetName = WebviewResourceMappingHelper.getInstance().getLocalAssetPath(resourceUrl); | |
if (StringUtils.isNotEmpty(assetName)) { | |
String mimeType = WebviewResourceMappingHelper.getInstance().getMimeType(fileExtension); | |
if (StringUtils.isNotEmpty(mimeType)) { | |
try { | |
Log.e(TAG, assetName); | |
return WebviewResourceMappingHelper.getWebResourceResponseFromAsset(assetName, mimeType, encoding); | |
} catch (IOException e) { | |
return super.shouldInterceptRequest(view, request); | |
} | |
} | |
} | |
String localFilePath = WebviewResourceMappingHelper.getInstance().getLocalFilePath(resourceUrl); | |
if (StringUtils.isNotEmpty(localFilePath)) { | |
String mimeType = WebviewResourceMappingHelper.getInstance().getMimeType(fileExtension); | |
if(StringUtils.isNotEmpty(mimeType)){ | |
try { | |
Log.e(TAG, localFilePath); | |
return WebviewResourceMappingHelper.getWebResourceResponseFromFile(localFilePath, mimeType, encoding); | |
} catch (FileNotFoundException e) { | |
return super.shouldInterceptRequest(view,request); | |
} | |
} | |
} | |
} | |
if (fileExtension.endsWith("jpg")) { | |
try { | |
InputStream inputStream = ImageUtils.readFromCacheSync(resourceUrl); | |
if (inputStream != null) { | |
return new WebResourceResponse("image/jpg", "UTF-8", inputStream); | |
} | |
} catch (Exception e) { | |
return super.shouldInterceptRequest(view,request); | |
} | |
} | |
return super.shouldInterceptRequest(view,request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment