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

前端 未结 5 1884
别那么骄傲
别那么骄傲 2020-12-10 12:22

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.8         


        
相关标签:
5条回答
  • 2020-12-10 12:57

    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

    0 讨论(0)
  • 2020-12-10 12:58

    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 ); 
    
    0 讨论(0)
  • 2020-12-10 13:00
     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());
    
    0 讨论(0)
  • 2020-12-10 13:03

    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 讨论(0)
  • 2020-12-10 13:06

    try this :

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

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

    0 讨论(0)
提交回复
热议问题