3

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?

Squonk
  • 48,735
  • 19
  • 103
  • 135
kingmaple
  • 4,200
  • 5
  • 32
  • 44

1 Answers1

-2

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);
}
bitle
  • 415
  • 2
  • 5
  • 5
    This does not actually work, did you test it? Android does not execute shouldOverrideUrlLoading() when it loads resources (like pictures and JavaScript). There is a method for interrupting this loading process called shouldInterceptRequest(), but it is available only on Android API 11 and above and I need it for Android 2.2 (API 8 and above). – kingmaple Aug 29 '11 at 07:55
  • Try this method http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) – bitle Aug 29 '11 at 18:08
  • Think this post is about the same: http://stackoverflow.com/questions/4780899/intercept-and-override-http-requests-from-webview Basically api lvl 11 supports it, anything prior is just screwed :) – Warpzit Oct 31 '11 at 08:50
  • does not work. Your override of shouldOverrideUrlLoading does nothing. – Victor Ionescu May 12 '14 at 15:42