我有一个webView,我从连接中获取webContent为String以获得响应并将其更改为插入数据库并在Main2Activity中获取 但是就像我说的那样,将其更改为base64 encodeToString之后,即使我没有将其更改为base64,图片也不会显示。 供离线使用
public static String getHtml(String url) throws IOException {
// Build and set timeout values for the request.
URLConnection connection = (new URL(url)).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
// Read and store the result line by line then return the entire string.
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder html = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
html.append(line);
}
in.close();
return html.toString();
}
这是我获取字符串的方式 我的字符串看起来像在日志中
<!DOCTYPE html><html id="js-context" class="js-off is-not-modern id--signed-out" lang="en" data-page-path="/money/2019/dec/10/starbucks-gift-card-refused-franchise-stores"><head><!-- __ __ _ _ _ \ \ / /__ __ _ _ __ ___ | |__ (_)_ __(_)_ __ __ _ \ \ /\ / / _ \ / _` | '__/ _ \ | '_ \| | '__| | '_ \ / _` | \ V V / __/ | (_| | | | __/ | | | | | | | | | | | (_| | \_/\_/ \___| \__,_|_| \___| |_| |_|_|_| |_|_| |_|\__, | |___/ Ever thought about joining us? https://workforus.theguardian.com/careers/digital-development/ ---><title>The Starbucks gift card I bought is useless | Money | The Guardian</title><meta charset="utf-8"><meta name="description" content="It won’t accept it at the Leicester University franchise and the nearest alternative is an hour away"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><meta name="format-detection" content="telephone=no"/><meta name="HandheldFriendly" content="True"/><meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"><link rel="dns-prefetch" href="https://assets.guim.co.uk/"/><link rel="dns-prefetch" href="https://i.guim.co.uk"/><link rel="dns-prefetch" href="https://api.nextgen.guardianapps.co.uk"/><link rel="dns-prefetch" href="https://hits-secure.theguardian.com"/><link rel="dns-prefetch" href="//j.ophan.co.uk"/><link rel="dns-prefetch" href="//ophan.theguardian.com"/><link rel="dns-prefetch" href="//phar.gu-web.net"/><link rel="dns-prefetch" href="//www.google-analytics.com"/><link rel="preconnect" href="https://assets.guim.co.uk/"/><link rel="preconnect" href="https://i.guim.co.uk"/><link rel="preconnect" href="//interactive.guim.co.uk"/><link rel="preconnect" href="//www.google-analytics.com"/><link rel="dns-prefetch" href="//sb.scorecardresearch.com"/><link rel="apple-touch-icon" sizes="152x152" href="https://assets.guim.co.uk/images/favicons/fee5e2d638d1c35f6d501fa397e53329/152x152.png"/><link rel="apple-touch-icon" sizes="144x144" href="https://assets.guim.co.uk/images/favicons/1fe70b29879674433702d5266abcb0d4/144x144.png"/><link rel="apple-touch-icon" sizes="120x120" href="https://assets.guim.co.uk/images/favicons/c58143bd2a5b5426b6256cd90ba6eb47/120x120.png"/><link rel="apple-touch-icon" sizes="114x114" href="https://assets.guim.co.uk/images/favicons/68cbd5cf267598abd6a026f229ef6b45/114x114.png"/><link rel="apple-touch-icon" sizes="72x72" href="https://assets.guim.co.uk/images/favicons/873381bf11d58e20f551905d51575117/72x72.png"/><link rel="apple-touch-icon-precomposed" href="https://assets.guim.co.uk/images/favicons/6a2aa0ea5b4b6183e92d0eac49e2f58b/57x57.png"/><link rel="manifest" href="/2015-06-24-manifest.json" crossorigin="use-credentials"><link rel="amphtml" href="https://amp.theguardian.com/money/2019/dec/10/starbucks-gift-card-refused-franchise-stores"><link rel="shortcut icon" type="image/png" href="https://assets.guim.co.uk/images/favicons/46bd2faa1ab438684a6d4528a655a8bd/32x32.ico"/><link rel="canonical" href="https://www.theguardian.com/money/2019/dec/10/starbucks-gift-card-refused-franchise-stores"/><meta name="apple-mobile-web-app-title" content="Guardian"/><meta name="application-name" content="The Guardian"/><meta name="msapplication-TileColor" content="#052962"/><meta name="theme-color" content="#052962"><meta name="msapplication-TileImage" content="https://assets.guim.co.uk/images/favicons/023dafadbf5ef53e0865e4baaaa32b3b/windows_tile_144_b.png"/><link rel="publisher" href="https://plus.google.com/113000071431138202574"/><meta name="author" content="Rebecca Smithers"/><meta name="thumbnail" content="https://i.guim.co.uk/img/media/843b4d2dc4f73319eba6210d4d68509119b702c2/0_0_4096_2458/master/4096.jpg?width=620&quality=85&auto=format&fit=max&s=adbfbb41f5b50749f72609024e473493"/><meta name="keywords" content="Consumer rights,Money,Starbucks,Retail industry"/><meta name="news_keywords" content="Consumer rights,Money,Starbucks,Reta
这是主要活动中的webView代码,我通过ID号从数据库中获取字符串
web_View = findViewById(R.id.webView);
web_View.setWebViewClient(new WebViewClient());
if (CheckInternet.isNetwork(getApplicationContext())) {
if(url2!=null) {
Log.e("url2", url2);
web_View.loadUrl(url2);
}
} else {
int data = getUrl.getIntExtra("data",0);
String a = FavNewsDatabase.getInstance(getApplicationContext()).noteDao().loadSingle(data);
Log.e("data2",a);
String encodedHtml = Base64.encodeToString(a.getBytes(), Base64.NO_PADDING);
web_View.loadData(encodedHtml, "text/html", "base64");
}
如何打开图像并谢谢