json值的img src属性,在android TextView中显示蓝色小框
我已检索到JSON值以在androidjson值的img src属性,在android TextView中显示蓝色小框,android,json,textview,Android,Json,Textview,我已检索到JSON值以在androidTextView 这是我的示例JSON值 "introtext": "The District Administration Office has sealed the case was underway.<img src="images/dec_09_Lazimpat_road_b.JPG" alt="" />" 我过去常常这样转义HTML属性和样
TextView
这是我的示例JSON值
"introtext": "The District Administration Office has sealed the case was underway.<img src="images/dec_09_Lazimpat_road_b.JPG" alt="" />"
我过去常常这样转义HTML属性和样式。
String str = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(stringValue);
我已经将值设置为TextView
,如下所示
textView.setText(Html.fromHtml(str));
在此之前一切正常,但TextView
中的text
如下图所示
此蓝色小框显示在img src
属性所在的位置。我已经浪费了太长时间试图删除这个。请帮助我如何从TextView
中删除此框。任何帮助都将不胜感激。请尝试以下代码:
String s = "The District Administration Office has sealed the case was underway.<img src="images/dec_09_Lazimpat_road_b.JPG" alt="" />";
String str = Html.fromHtml(s).toString();
///// Remove all img tags using Regular Expression
str = str.replaceAll("[<](/)?img[^>]*[>]", "");
textView.setText(Html.fromHtml(str));
String s=“地区行政办公室已封存正在审理的案件。img src=“images/dec_09_Lazimpat_road_b.JPG”alt=”“/”;
String str=Html.fromHtml.toString();
/////使用正则表达式删除所有img标记
str=str.replaceAll(“[]*[>]”,“”);
setText(Html.fromHtml(str));
要显示包含html内容的图像,也需要使用&from html。使用ImageGetter需要下载图像,将其转换为位图并在文本视图中显示,使用Html内容和Html.ImageGetter的实例
下面我给出了解决问题的方法
建议
我建议您对需要显示html文本的内容使用WebView
HTMLImageDrawable
public class HTMLImageDrawable extends BitmapDrawable {
protected Drawable drawable;
@Override
public void draw(Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
}
HtmlImageParser
public class HtmlImageParser implements ImageGetter {
public static final String LOG = HtmlImageParser.class.getName();
Context c;
View container;
public HtmlImageParser(View t, Context c) {
this.c = c;
this.container = t;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Drawable getDrawable(String source) {
try {
HTMLImageDrawable urlDrawable = new HTMLImageDrawable();
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(
urlDrawable);
if (Build.VERSION.SDK_INT < 11) {
asyncTask.execute(source);
} else {
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, source);
}
return urlDrawable;
} catch (Exception e) {
Log.e(LOG, e.getMessage());
}
return null;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
HTMLImageDrawable urlDrawable;
public ImageGetterAsyncTask(HTMLImageDrawable d) {
this.urlDrawable = d;
}
@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}
@Override
protected void onPostExecute(Drawable result) {
try {
if (urlDrawable != null) {
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(),
0 + result.getIntrinsicHeight());
urlDrawable.drawable = result;
HtmlImageParser.this.container.invalidate();
}
} catch (Exception e) {
Log.e(LOG, e.getMessage());
}
}
public Drawable fetchDrawable(String urlString) {
try {
// InputStream is = fetch(urlString);
URL imageURL = new URL(urlString);
InputStream inputStream = imageURL.openStream();
Drawable drawable = Drawable.createFromStream(inputStream, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(),
0 + drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}
}
}
公共类HtmlImageParser实现ImageGetter{
public static final String LOG=HtmlImageParser.class.getName();
上下文c;
视图容器;
公共HtmlImageParser(视图t,上下文c){
这个.c=c;
这个容器=t;
}
@TargetApi(构建版本代码蜂窝)
公共可绘制getDrawable(字符串源){
试一试{
HTMLImageDrawable urlDrawable=新的HTMLImageDrawable();
ImageGetterAsyncTask asyncTask=新ImageGetterAsyncTask(
(可绘制);
if(Build.VERSION.SDK_INT<11){
asyncTask.execute(源代码);
}否则{
asyncTask.executeOnExecutor(asyncTask.THREAD\u POOL\u EXECUTOR,源);
}
返回可提取;
}捕获(例外e){
Log.e(Log,e.getMessage());
}
返回null;
}
公共类ImageGetterAsyncTask扩展异步任务{
HTMLImageDrawable URLDawable;
公共图像GetterAsynctask(HTMLImageDrawable d){
this.urlDrawable=d;
}
@凌驾
受保护的可抽出式doInBackground(字符串…参数){
字符串源=参数[0];
返回可提取的(源);
}
@凌驾
受保护的void onPostExecute(可提取结果){
试一试{
如果(URLDAWABLE!=null){
urlDrawable.setBounds(0,0,0+result.getIntrinsicWidth(),
0+result.getIntrinsicHeight());
urlDrawable.drawable=结果;
HtmlImageParser.this.container.invalidate();
}
}捕获(例外e){
Log.e(Log,e.getMessage());
}
}
公共可提取可提取(字符串urlString){
试一试{
//InputStream is=fetch(urlString);
URL imageURL=新URL(URL字符串);
InputStream InputStream=imageURL.openStream();
Drawable Drawable=Drawable.createFromStream(inputStream,“src”);
drawable.setBounds(0,0,0+drawable.getIntrinsicWidth(),
0+可绘制的.getIntrinsicHeight());
回拉;
}捕获(例外e){
返回null;
}
}
}
}
添加以上两个类
将Html文本设置为TextView
TextView htmltext = (TextView) view.findViewById(R.id.htmltext);
HtmlImageParser clsUrlimageparser = new HtmlImageParser(htmltext.getRootView(), getActivity());
String imageviewBaseURL = "http://tfwiki.net/mediawiki/images2/thumb/8/8c/";
//For multiple image if base url is fix & image path is relative
String img1 = imageviewBaseURL + "AOE_optimus_reformatted.jpg/180px-AOE_optimus_reformatted.jpg";
String img2 = imageviewBaseURL + "AnimeMach.jpg/82px-AnimeMach.jpg";
String text = "Android test image text" +
"<br /> <img style='width:304px;height:228px;' src='" + img1 + "'/>" +
"<br /> <img style='width:304px;height:228px;' src='" + img2 + "'/>";
htmlSpan = Html.fromHtml(text, clsUrlimageparser, null);
htmltext.setText(htmlSpan);
TextView htmltext=(TextView)view.findViewById(R.id.htmltext);
HtmlImageParser clsUrlimageparser=新的HtmlImageParser(htmltext.getRootView(),getActivity());
字符串imageviewBaseURL=”http://tfwiki.net/mediawiki/images2/thumb/8/8c/";
//对于多个图像,如果基本url是固定的&图像路径是相对的
字符串img1=imageviewBaseURL+“AOE_optimus_reformatted.jpg/180px-AOE_optimus_reformatted.jpg”;
字符串img2=imageviewBaseURL+“AnimeMach.jpg/82px AnimeMach.jpg”;
String text=“Android测试图像文本”+
“
”+
“
”;
htmlSpan=Html.fromHtml(text,clsUrlimageparser,null);
htmltext.setText(htmlSpan);
此代码
public Drawable fetchDrawable(String urlString) {
try {
URL imageURL = new URL(urlString);
InputStream inputStream = imageURL.openStream();
Drawable drawable = Drawable.createFromStream(inputStream, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(),
0 + drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}
给我的信息如下
html图像将包含实时url?@user1140237是的,他们中的一个也有……一些img src标记中包含url。我们能为这些做些什么?我正在等待其他答案,所以我会尽快检查它是否正确。谢谢你的回答。。太棒了…你想保留这个网址吗?是的,如果有。。在替换字符串之前,有没有办法抓取URL?但是src是相对的,即使你让链接在TextView中可点击,也没有用。现在可以了。让我们不要优先考虑src属性。你的解决方案奏效了。再次感谢。src
标记中的图像路径是相对的,而不是绝对的。因此图像不会被下载。@EricB。好的,那么基本url呢?我们不知道OP是否有基本url。其次,不鼓励使用DefaultHttpClient
,您应该使用HttpURLConnection
。@EricB。谢谢你的意见。。我知道&如果这种方法有用,您可以更新它。如果有时间,我也会更新它:)感谢您建议使用WebView
我使用了另一个库来解析。