android Not allowed to load local resource:file:///android_asset

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

Java:

  • Android Studio AVD error launching
  • set mouse position on rooted android device
  • Cannot start Emulator in android studio 2.0
  • What's the best way to uniquely identify Android users to a server?
  • Android listView find the amount of pixels scrolled
  • how i can break things with Fragments with setRetainInstance(true) and adding them to backstack?
  • 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

  • Create borders on a android view in drawable xml, on 3 sides?
  • How can I get the options menu of my Activity?
  • Android: Is it possible to display video thumbnails?
  • Emulator's Location simulation not working
  • How to achieve a full-screen dialog as described in material guidelines?
  • how to keep RecyclerView always scroll bottom
  • 4 Solutions collect form web for “android Not allowed to load local resource:file:///android_asset”

    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 ); 
    

    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

    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");
    
    }
    

    try this :

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

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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.