最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load a webpage on WebView from locally-created folder? - Stack Overflow

programmeradmin3浏览0评论

Working with Android-Studio, I am trying to use WebView to load a local html file that contains javascript.

First, I created an assets folder inside the Java folder. Then, I created two folders inside assets folder: html and scripts. I put my index.html page inside html folder and the index.js file inside scripts folder. The two paths look like the following in Android-Studio:

  1. app/java/assets/html/index.html
  2. app/java/assets/scripts/index.js

Now, when I try to load the index.html, I use the following syntax:

myWebView.loadUrl("file:///assets/html/index.html");

but it is not working. I also used each of the following:

myWebView.loadUrl("file://assets/html/index.html");

myWebView.loadUrl("file://android_assets/html/index.html");

myWebView.loadUrl("file:///android_assets/html/index.html");

Still, when the page loads, it shows "Webpage not available"!!

The app works fine, the intent start and a webpage loads with the above warning message.

  1. What is right syntax to load the webpage from a local-created folder ?
  2. What is the right syntax for loading index.js inside the index.html ?

Here is the code:

    public class DisplayMessageActivity extends Activity {

    private WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_assets/html/index.html"); // doesn't work
        // myWebView.loadUrl(""); // works
    }

UPDATE

Here is the index.html :

<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>

No javascript at the moment.

Working with Android-Studio, I am trying to use WebView to load a local html file that contains javascript.

First, I created an assets folder inside the Java folder. Then, I created two folders inside assets folder: html and scripts. I put my index.html page inside html folder and the index.js file inside scripts folder. The two paths look like the following in Android-Studio:

  1. app/java/assets/html/index.html
  2. app/java/assets/scripts/index.js

Now, when I try to load the index.html, I use the following syntax:

myWebView.loadUrl("file:///assets/html/index.html");

but it is not working. I also used each of the following:

myWebView.loadUrl("file://assets/html/index.html");

myWebView.loadUrl("file://android_assets/html/index.html");

myWebView.loadUrl("file:///android_assets/html/index.html");

Still, when the page loads, it shows "Webpage not available"!!

The app works fine, the intent start and a webpage loads with the above warning message.

  1. What is right syntax to load the webpage from a local-created folder ?
  2. What is the right syntax for loading index.js inside the index.html ?

Here is the code:

    public class DisplayMessageActivity extends Activity {

    private WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_assets/html/index.html"); // doesn't work
        // myWebView.loadUrl("http://www.example."); // works
    }

UPDATE

Here is the index.html :

<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>

No javascript at the moment.

Share Improve this question edited Sep 9, 2016 at 11:22 McLan asked Sep 9, 2016 at 10:35 McLanMcLan 2,6989 gold badges55 silver badges91 bronze badges 7
  • 1 please show content of your index.html. You might have incorrect reference to index.js. Please use this format mWebView.loadUrl("file:///android_asset/html/index.html"); To isolate issue with index.js. Please create a hello_world.html and try loading in html to see if that's loaded correctly – Amod Gokhale Commented Sep 9, 2016 at 10:43
  • @AmodGokhale : I am already using a raw hello_world html file (please check the update). At the moment, I am just trying to load the correct page. But it is not working !! – McLan Commented Sep 9, 2016 at 10:53
  • 1 never mind.. for android studio file should be located @ /app/src/main/assets/html/index.html and then load using file:///android_asset/html/index.html – Amod Gokhale Commented Sep 9, 2016 at 10:58
  • @AmodGokhale .. No error at the logcat .. – McLan Commented Sep 9, 2016 at 11:02
  • 1 no.. you have created assets folder inside java.. it should be /src/main/assets/... file name.. your path is app/java/assets – Amod Gokhale Commented Sep 9, 2016 at 11:06
 |  Show 2 more ments

2 Answers 2

Reset to default 4

Create assets folder under main ( not inside java ) and then use

myWebView.loadUrl("file:///android_asset/html/index.html");

not

myWebView.loadUrl("file:///android_asset**s**/html/index.html");

Use above answer just replace

webView.loadUrl("file://android_asset/index.html");

with webView.loadUrl("file:///android_asset/index.html");

发布评论

评论列表(0)

  1. 暂无评论