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_asse开发者_如何学编程t/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?
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);
}
精彩评论