Reputation: 1331
I have followed the instructions and information found in this thread:
Webview load html from assets directory
Which lead me to generate the following code:
The html file, patchnotes.html:
<!DOCTYPE html>
<html>
<head>
<title>Hi there</title>
</head>
<body>
This is a page
a simple page
</body>
</html>
The XML reference to the webveiw I am using:
<WebView
android:id="@+id/webview"
android:visibility="gone"
android:layout_marginLeft="30dp"
android:layout_marginTop="220dp"
android:layout_width="200dp"
android:layout_height="300dp"></WebView>
The Java code relevant to displaying the webview:
private void changeLog() {
final View newsPanel = (View) findViewById(R.id.newsPanel);
final TextView titleChangeLog = (TextView) findViewById(R.id.titleChangeLog);
final WebView webview = (WebView) findViewById(R.id.webview);
newsPanel.setVisibility(View.VISIBLE);
titleChangeLog.setVisibility(View.VISIBLE);
webview.setVisibility(View.VISIBLE);
toggleMenu(newsPanel);
}
public class ViewWeb extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.loadUrl("file:///android_asset/patchnotes.html");
}
}
I suspect perhaps it is something to do with the class ViewWeb never being called, but there is nothing at all in the example I linked above to suggest that you need to.
What happens when I execute this code is that nothing is displayed. There is no error, it just doesn't display any of the content of the html file.
Upvotes: 0
Views: 147
Reputation: 3734
If you want access file from assets folder use following code.This will list all the files in the assets folder:
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
This to open a certian file:
InputStream input = assetManager.open(assetName);
EDIT
String htmlFilename = "patchnotes.html";
AssetManager mgr = getBaseContext().getAssets();
try {
InputStream in = mgr.open(htmlFilename, AssetManager.ACCESS_BUFFER);
String htmlContentInStringFormat = StreamToString(in);
in.close();
wv.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);
} catch (IOException e) {
e.printStackTrace();
}
public static String StreamToString(InputStream in) throws IOException {
if(in == null) {
return "";
}
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
}
return writer.toString();
}
Upvotes: 1