-1

The javascript code in my html file can run correctly when I use WebView.load URL to load that html file. But i want to use WebView.loadData ,then i read that html file in a String ,and use WebView.loadData. The JavaScript code in html can't run successfully. who can help me? thanks.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    try {
        InputStream is=this.getAssets().open("COPY-ME.html");
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        int i=1;
        while ((i=is.read())!=-1) {
            baos.write(i);
        }
        data=baos.toString();

    } catch (IOException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    webView.loadData(data, "text/html", "utf-8");

1 Answer 1

1

try to load your html file using below code

webView.loadUrl("file:///android_asset/COPY-ME.html");

Its advised to keep your resource name in small letters.

Sign up to request clarification or add additional context in comments.

2 Comments

maybe you don't read my question carefully. the point is that i want use webView.loadData for some reason.
Thank you very much,this link is very useful for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.