How to link multiple HTML pages resides in assets folder in android?
I am new to Android.
I want to display HTML pages in android emulator. I put my "test1.html" and "test2.html" files in the "assets" folder and managed to display "test1.html" file in Android by using the following code.
package com.example.helloandroid;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
try {
InputStream fin = g开发者_开发知识库etAssets().open("test1.html");
byte[] buffer = new byte[fin.available()];
fin.read(buffer);
fin.close();
webview.loadData(new String(buffer), "text/html", "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now I want to click a button or a link in the "test1.html" file and open the "test2.html" file.How can I link those two HTML pages?
Thanks in advance.
Button
or Link
in test1.html
should just work like normal html file. Make sure when you provide the path for test2.html
is right with respect to test1.html
since they are in assets directory.
Edit your code like this
<a href="file:///android_asset/test2.html">Next Page</a>
I was trying to do a similar thing where I wanted to load 1 asset that lead to another asset. The issue is that you need to extract both asssets or you will get the webpage not avail error. So yes, you would need to create an input stream for test2 as well.
There are other ways to extract assets other than using input streams. You will have to play around with making a directory in your apk and moving the file to a certain path, and using that path. It's a little confusing but can help if you plan to load many local assets in the future.
I couldn't find a neat solution that works for all devices. But I would recommend you to first copy the files from Assets to Internal Directory (getFilesDir()). And then perform
webView.loadUrl("file://"+getFilesDir()+"/myHtml.html");
精彩评论