【问题标题】:Webview load images from file but not from web in androidWebview从文件中加载图像,而不是从android中的网络加载图像
【发布时间】:2014-10-29 12:25:03
【问题描述】:

您好,我想将 HTML 数据加载到 WiebView 中。我的 html 数据包含两张图片,一张在资产文件夹中,另一张位于网络上。我的代码是这样的:

String html  = "<html><body><img src="image1.jpg"><img src="http://www.example.com/images/image2.jpg"></body></html>";

webView = (WebView) rootView.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);

webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8",null);

但是webview只加载第一张图片,不加载web图片。谁能帮我解决这个问题?

【问题讨论】:

  • http://www.example.com/images/image2.jpg 不是 jpg 而是 html。
  • @greenapps 我使用这个虚拟 url 只是作为一个例子。
  • 不要那样做。您不能将其用作示例,因为它是一个现有的 url,由于我告诉您的原因,它在您的代码中不起作用。这种方式非常混乱。

标签: android image webview load


【解决方案1】:

您的代码没有问题(未转义的引号除外),但您可能忘记为 webview 请求互联网许可。将此添加到您的清单文件中:

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

  • 我已经在我的清单文件中获得了这个权限,并且我的应用程序的其他部分可以毫无问题地连接到互联网。
【解决方案2】:

首先在清单中添加互联网权限。 秒写:

webView.loadDataWithBaseURL("http:///android_asset/", html, "text/html", "UTF-8",null);

不是:

webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8",null);

因为http显示网页,文件显示内存

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 2016-06-08
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多