gpt4 book ai didi

android - 在 textview 中加载 Html.fromHtml 中的图像(http url images,Base64 url​​ images)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:05 26 4
gpt4 key购买 nike

我有一个 TextView ,其中显示论坛帖子的内容,该帖子是使用 rte 在网站上输入的,内容涉及图像,类型为 web url 和类型 Base64.. The Html.fromHtml 的默认实现将所有 img 标签替换为小方 block ..

我在 SO 上寻找使用 Html.fromHtml 方法从 url 加载图像的解决方案,结果证明有办法做到这一点,我们可以将 ImageGetter 传递给函数。我找到了 this很棒的答案,它实现了 url 获取部分,但是当内容包含 Base64 的图像时,这会失败并导致应用程序崩溃。

我寻找了一种为 Base64 src 创建图像的方法,但没有一个解决方案有效,如果有人实现了整个解决方案,那就太好了。如果有人只有 Base64 部分,请提供我将两者整合..

最佳答案

在花费数小时之后,我终于找到了 Base64 图像的解决方案。我将在此处发布完整的解决方案。

再次感谢https://stackoverflow.com/a/15617341/1114536对于基本答案..

原来我用作引用的答案只是 this asnwer 的副本..

URLDrawable.java

import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class URLDrawable extends BitmapDrawable {
// the drawable that you need to set, you could set the initial drawing
// with the loading image if you need to
protected Drawable drawable;

@Override
public void draw(Canvas canvas) {
// override the draw to facilitate refresh function later
if(drawable != null) {
drawable.draw(canvas);
}
}
}

URLImageParser.java

import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.util.Base64;
import android.view.View;

public class URLImageParser implements ImageGetter {
Context context;
View container;

public URLImageParser(View container, Context context) {
this.context = context;
this.container = container;
}

public Drawable getDrawable(String source) {
if(source.matches("data:image.*base64.*")) {
String base_64_source = source.replaceAll("data:image.*base64", "");
byte[] data = Base64.decode(base_64_source, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable image = new BitmapDrawable(context.getResources(), bitmap);
image.setBounds(0, 0, 0 + image.getIntrinsicWidth(), 0 + image.getIntrinsicHeight());
return image;
} else {
URLDrawable urlDrawable = new URLDrawable();
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
asyncTask.execute(source);
return urlDrawable; //return reference to URLDrawable where We will change with actual image from the src tag
}
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;

public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}

@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}

@Override
protected void onPostExecute(Drawable result) {
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight()); //set the correct bound according to the result from HTTP call
urlDrawable.drawable = result; //change the reference of the current drawable to the result from the HTTP call
URLImageParser.this.container.invalidate(); //redraw the image by invalidating the container
}

public Drawable fetchDrawable(String urlString) {
try {
InputStream is = (InputStream) new URL(urlString).getContent();
Drawable drawable = Drawable.createFromStream(is, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}
}
}

用法:

TextView comment_content_container = ((TextView)findViewById(R.id.comment_content));
comment_content_container.setText(Html.fromHtml(comment.content, new URLImageParser(comment_content_container, this), null));

如果有人知道 Base64 的更好的正则表达式,请回复我会更新答案..

关于android - 在 textview 中加载 Html.fromHtml 中的图像(http url images,Base64 url​​ images),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22936653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com