Reputation: 6039
My code opens a index.html file in a webView but failed to open the css and js files, the errors I am getting are:
E/SysUtils: ApplicationContext is null in ApplicationStatus E/chromium: [ERROR:browser_gpu_channel_host_factory.cc(258)] Failed to init browser shader disk cache. E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
all the files referenced to in the <head>
of the index.html are located in the assets folder as siblings of index.html
package au.com.totalcareauto.webappandroid1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
WebView wv = (WebView) findViewById(R.id.wv);
wv.setWebViewClient(new WebViewClient());
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
String summary = null;
String path = "file:///assets/";
try {
summary = getStringFromFile("index.html");
} catch (Exception e) {
e.printStackTrace();
}
wv.loadDataWithBaseURL(path ,summary,"text/html","utf-8",null);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
public String getStringFromFile(String filePath) throws Exception {
File fl = new File(filePath);
String ret = convertStreamToString(this.getAssets().open(filePath));
return ret;
}
}
<head>
<meta charset="UTF-8">
<title>RRR</title>
<link type="text/css" rel="stylesheet" href="jquery.mobile-1.4.5.css"/>
<link type="text/css" rel="stylesheet" href="index.css"/>
<script type="text/javascript" src="jquery-1.11.3.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.5.js"></script>
<meta name="viewport" content="width=device-width"/>
</head>
Upvotes: 1
Views: 5086
Reputation: 856
The log does not reflect the cause the loading failure.
The issue is you have wrong path:
String path = "file:///assets/";
Which should be:
String path = "file:///android_asset/";
Upvotes: 1