-
-
Save chethann/3edcc6f702430e42fe4f018546766481 to your computer and use it in GitHub Desktop.
@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); | |
} |
Can you provide WebviewResourceMappingHelper?
Yes, could you provide the source code of WebviewResourceMappingHelper
?
Hi, I have added code for WebviewResourceMappingHelper class. Hope this will help.
https://gist.github.com/chethann/bfa24f3517864a4be39757b987d58a17
Hi, I read your code and have a question, how can you improve webview loading since you cannot replace the remote resource with local resource when you surf others website that not yours?
AFAIK when we surf others website we cannot do much as they define there very own cache policy and web view by default manage cache throughout the lifetime of running a application.
Yes, this solution is designed to be used inside a hybrid app (some pages in native and some in web)
please share ImageUtils
Hi,
Can you share your Intentservice class where you are hitting json file and syncing resouces
Hi I am new to android development.
I am looking for an webview app with cachemode, to make my webview load faster.
I Studied your Post and it is what i am expecting, Can you please share me how to embed these into Mainactivity.java
I tried but i ended with Errors!
Few errors like Cannot Resolve WebviewResourceMappingHelper, Like wise I get Errors while using the above code!
I looking for your Support.