「Android/webview/html動的変更」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「*java String html = "<<nowiki />html><<nowiki />head><<nowiki />head><<nowiki />body>test</body></html>"; webview.loadDataWithBaseURL("file:///android_asset/", html, "t...」) |
|||
行1: | 行1: | ||
− | + | ==サンプルコード== | |
+ | src/main/assets/index.htmlにhtmlソースを置く | ||
String html = "<<nowiki />html><<nowiki />head><<nowiki />head><<nowiki />body>test</body></html>"; | String html = "<<nowiki />html><<nowiki />head><<nowiki />head><<nowiki />body>test</body></html>"; | ||
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null); | webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null); | ||
+ | |||
+ | ==baseUrlを指定したサンプルコード== | ||
+ | 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);} | ||
+ | webview.loadDataWithBaseURL("ttp://hoge.example.com", html, "text/html", "UTF-8", null); |
2019年6月28日 (金) 12:49時点における版
サンプルコード
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);
baseUrlを指定したサンプルコード
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);} webview.loadDataWithBaseURL("ttp://hoge.example.com", html, "text/html", "UTF-8", null);