Loading html file to webview on android from assets folder using Android Studio

前端 未结 3 1534
走了就别回头了
走了就别回头了 2020-12-01 09:34

I\'m using Android Studio/Gradle.

app\\src\\main\\android_asset folder has the file called chart.html..

I\'m trying to load this file to my webview like this

3条回答
  •  难免孤独
    2020-12-01 09:52

    Answer from Gugelhupf but with raw resource.
    Advantage from this solution: you keep translation working!

    WebView webView = findViewById(R.id.about_text);
    try {
      InputStream inputStream = getResources().openRawResource(R.raw.about);
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      StringBuilder stringBuilder = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
      }
      webView.loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "UTF-8", null);
    } catch (IOException e) {
      e.printStackTrace();
    }
    

提交回复
热议问题