18

I'm trying to load an html page from a server the page contains a script that links to android sdcard .js file.

Java:

String url ="http://192.168.84.86:8080/test/maw/js_load_test.html";
this.loadUrl(url);

js_load_test.html:

<script src="file:///sdcard/test.js"></script>

I've also tried :

<script src="file:///android_asset/www/js/test.js"></script>

test.js file exists and js file path is correct. But in logcat: file:///sdcard/test.js:

03-18 13:01:12.467: E/Web Console(26189): Not allowed to load local resource: file:///sdcard/test.js at :0

file:///android_asset/www/js/test.js:

03-18 13:01:53.467: E/Web Console(26189): Not allowed to load local resource: file:///android_asset/www/js/test.js at :0

Simon C
  • 9,458
  • 3
  • 36
  • 55
explorer
  • 449
  • 2
  • 10
  • 19

5 Answers5

17

This may help

https://groups.google.com/group/android-developers/browse_thread/thread/e20e87d2faf9ff41?pli=1#

webview.loadDataWithBaseURL( "file:///android_asset/", html, "text/html", 
"utf-8", null ); 
Sindre
  • 3,880
  • 2
  • 26
  • 39
  • thanks for your help, but I want use loadUrl(), because I find that loadDataWithBaseURL does not support the HTML5 feature of localstorage, maybe I need something else config? – explorer Mar 19 '12 at 00:46
1

If you getting "Not allowed to load local resource: file:///android_asset/index.html" error most probably your problem is putting assets folder in wrong location.

For gradle project store your assets folder under src/main/ directory by the java and res folders. Like this:

project-folder -|project_name
--|build
--|src
---|assets
---|gen
---|java
---|res

For maven project store yout assets under res/ directory

Defuera
  • 5,356
  • 3
  • 32
  • 38
1

to fix this create a folder in "main" called "android_asset" and inside android_asset folder create another folder called "assets" and place your html file in assets and call using this mWebView.loadUrl("file:///android_asset/YOUR HTML FILE.html");

I had the same problem and even though I dont call mWebView.loadUrl("file:///android_asset/assets/YOUR HTML FILE.html"); it still some how works. which i find strange because thats where the html actually is!

So again your folders should look like this main/android_assets/assets/YOUR HTML.html and call with mWebView.loadUrl("file:///android_asset/YOUR HTML FILE.html");

Heres how my oncreate looks.

public class MainActivity extends ActionBarActivity {

private WebView mWebView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("file:///android_asset/index.html");

}
0
 String htmlStr="<html>..your html data in string..\"data/replace.js\" </html>";
 if (htmlStr.contains("\"data/replace.js\"")) {
     String html = data.replace("\"data/replace.js\"", "\"file:///storage/emulated/0/documents/HtmlContent/new_replace.js\"");
     ((Activity) mContext).runOnUiThread(() -> {
            loadDataWithBaseURL("file:///storage/emulated/0/documents/HtmlContent/", html, "text/html", "UTF-8", "");
      });
  }

Try following code to load Html content from sdcard.

 String fileName = "something.html";
 File lFile = new File(Environment.getExternalStorageDirectory() + fileName);
 webview.loadUrl("file:///" + lFile.getAbsolutePath());
Naresh Palle
  • 339
  • 3
  • 8
  • This is not an answer to the question. The OP asked how to load a local resource *from within an HTML file which itself has been loaded from an external server*. The OP did *not* ask how to load an HTML file from the SD card. – Binarus Apr 09 '18 at 11:31
0

try this :

this.loadUrl("file:///android_asset/www/js/test.js");

but there should be one folder name www->js in your assets folder.

Dhruvisha
  • 2,538
  • 1
  • 21
  • 31
  • is your html file in your asset folder or on some server? – Dhruvisha Mar 21 '12 at 11:05
  • @user1204703 I am too facing the same issue, I'm loading file content from SDCARD,webview loads the content but with JS & CSS. How to load JS & CSS from SDCARD with loadDataWithBaseURL() method. – sachin003 Apr 23 '13 at 13:30