Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在webview中更改src img_Android_Image_Webview_Android Webview - Fatal编程技术网

Android在webview中更改src img

Android在webview中更改src img,android,image,webview,android-webview,Android,Image,Webview,Android Webview,我有一个webview,在其中我从Web服务加载内容(新闻) 唯一的问题是,此内容中的我的图像具有如下src: <img src='images/myimage.png'> 当然,在我的网络视图中,我无法显示此图像 那么,如何更新所有图像的src?(要添加正确的url以显示它) 我像这样加载我的内容: String htmlNews = "<html><head><link rel=\"stylesheet\" t

我有一个webview,在其中我从Web服务加载内容(新闻)

唯一的问题是,此内容中的我的图像具有如下src:

<img src='images/myimage.png'> 

当然,在我的网络视图中,我无法显示此图像

那么,如何更新所有图像的src?(要添加正确的url以显示它)

我像这样加载我的内容:

                String htmlNews = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + htmlContent;
            webview.loadDataWithBaseURL("file:///android_asset/", htmlNews, "text/html", "UTF-8", null);
字符串htmlNews=”“+htmlContent;
webview.loadDataWithBaseURL(“file:///android_asset/,htmlNews,“text/html”,“UTF-8”,null);

将您的图像放入assets目录
例如:assets/img.png

然后使用以下内容加载您的html:

webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);
参考您的img,如:

<img src="img.png">

你也可以试试

String data = "<body>" + "<img src=\"large_image.png\"/></body>";
String data=“”+”;

w
ebView.loadDataWithBaseURL(“file:///android_asset/,数据,“文本/html”,“utf-8”,空)

在Webview中加载Html文件,并将图像放在资产文件夹中,然后使用Html读取该图像文件

<html>
  <table>
    <tr>
      <td>
        <img src="abc.gif" width="50px" alt="Hello">
      </td>
    </tr>
  </table>
</html>

按如下方式解析html代码:

String web = "<img src='images/myimage.png'>";
String replace = "src='";
String replaceWith = "src='http://example.com/";
String webParsed = web.replace(replace, replaceWith);
System.out.println(webParsed);
stringweb=”“;
字符串replace=“src=”;
字符串replaceWith=“src=”替换http://example.com/";
字符串WebPassed=web.replace(replace,replaceWith);
System.out.println(网页);
您要添加到src标记的前缀应该在哪里。

为了引起更多的注意,我将其保留在这里

存在一个定义基本URL的URL,您不必截取任何以路径开始或相对于路径的内容,HTML客户端将自动处理

val yourBaseUrl: String = ...
val originalContent: String = ...
val fixedContent = "<base href='$yourBaseUrl'>$originalContent"
webView.loadData(fixedContent, "text/html", "UTF-8")
val yourBaseUrl:String=。。。
val originalContent:String=。。。
val fixedContent=“$originalContent”
webView.loadData(固定内容,“文本/html”、“UTF-8”)

这些图像是否位于资产文件夹中?试着把图片放在与html文件相同的位置,而不是单独的文件夹中。Lol不,图片在我的网站上。答案是thx,但我发现其他事情更简单。。html标记:)
val yourBaseUrl: String = ...
val originalContent: String = ...
val fixedContent = "<base href='$yourBaseUrl'>$originalContent"
webView.loadData(fixedContent, "text/html", "UTF-8")