0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bisclavret
  • 1,327
  • 9
  • 37
  • 65

1 Answers1

1

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();
    }
Shahzeb
  • 3,696
  • 4
  • 28
  • 47
  • I need it to be gone, so I can make it appear upon request with this line: webview.setVisibility(View.VISIBLE); – Bisclavret Aug 27 '15 at 05:25
  • @Bisclavret is your file placed in assets ? – Shahzeb Aug 27 '15 at 05:27
  • As a point of interest, when the html file appears, it makes a white rectangular view on the screen, but the view contains no text. Does this mean there is something wrong with my html code? It displays correctly in a browser. – Bisclavret Aug 27 '15 at 05:28
  • Thanks Shazeb, yes it is. I used Android Studio to create the assets folder properly (right click app folder, new, folder, assets folder). I then copied the html file to this location, which is located here: app\src\main\assets – Bisclavret Aug 27 '15 at 05:29
  • @Bisclavret try to open the file in mobile browser and see if its correct – Shahzeb Aug 27 '15 at 05:29
  • In a browser on my pc, it is correct. I do not have access to this folder on a mobile device. It is integrated into the app, isn't it. – Bisclavret Aug 27 '15 at 05:31
  • @Bisclavret actually you cannot directly access assets folder – Shahzeb Aug 27 '15 at 05:32
  • I have copied the html file separately to my mobile device and opened in a browser. It displays the following text "This is a page a simple page". So it is working correctly. There is something wrong with my webview. – Bisclavret Aug 27 '15 at 05:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88061/discussion-between-shahzeb-and-bisclavret). – Shahzeb Aug 27 '15 at 05:41
  • 1
    The section of code added above works perfectly for me. I hope this helps anyone else who was relying on that other incomplete thread. Thank you Shahzeb, you are brilliant. – Bisclavret Aug 27 '15 at 08:42