facebook twitter hatena line email

「Android/webview/html動的変更」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(baseUrlを指定したサンプルコード)
(assetsにおいたindex.htmlを一部変更するサンプルコード)
行15: 行15:
 
             html = new String(buffer);
 
             html = new String(buffer);
 
  } catch (IOException e) {throw new RuntimeException(e);}
 
  } catch (IOException e) {throw new RuntimeException(e);}
html = html.replace("{hogehoge}", "hogehoge");
+
html = html.replace("{hogehoge}", "hogehoge");
 
  webview.loadDataWithBaseURL("ttp://hoge.example.com", html, "text/html", "UTF-8", null);
 
  webview.loadDataWithBaseURL("ttp://hoge.example.com", html, "text/html", "UTF-8", null);

2019年6月28日 (金) 13:16時点における版

サンプルコード

src/main/assets/index.htmlにhtmlソースを置く

String html = "<html><head><head><body>test</body></html>";
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

assetsにおいたindex.htmlを一部変更するサンプルコード

src/main/assets/index.htmlに置く

String html = "";
try {
           InputStream is = getAssets().open("index.html");
           int size = is.available();
           byte[] buffer = new byte[size];
           is.read(buffer);
           is.close();
           html = new String(buffer);
} catch (IOException e) {throw new RuntimeException(e);}
html = html.replace("{hogehoge}", "hogehoge");
webview.loadDataWithBaseURL("ttp://hoge.example.com", html, "text/html", "UTF-8", null);