Issue
I want to create a simple web based application on Android that loads certain large files, like Javascript and user interface elements locally, from 'assets' directory. However I am unable to use local assets directory when loading external HTML. So the external server would give something like this:
<script type="text/javascript" src="file:///android_asset/javascript.js"/>
And WebView would use that, even if the website itself came from external website. I know there are security risks involved, but is there a way to perhaps do that by only allowing specific URL's on applications end?
Is there any way I can do that?
Solution
First of all you have to copy all the assets somewhere (external storage, for instance).
Then, to filter URL's you should override method
public boolean shouldOverrideUrlLoading(WebView view, String url);
in WebViewClient class.
added: When your application starts, put the assets to /sdcard/somefolder/ see getExternalFilesDir()
in shouldOverrideUrlLoading override url to something like this
file:///sdcard/somefolder/<your filename here>
it should be as easy as
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "file://" + context.getExternalFilesDir().getAbsolutePath() + "/" + url;
super.shouldOverrideUrlLoading(view, url);
}
Answered By - bitle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.