android - 使用 TextView.setText(Html.fromHtml(string)) 将具有 html 图像标签的字符串转换为图像

标签 android android-layout

我有一个字符串,如下所示:

String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"

当我这样做的时候

TextView.setText(Html.fromHtml(text));

我需要在 Activity 中显示图像。

我不想使用 WebView。

请告诉我还有哪些其他方法可以实现这一目标。

最佳答案

你必须使用asynctask,在doInbackground()中打开连接,在onPostExecute()中将图像设置为textview

try {
        /* Open a new URL and get the InputStream to load data from it. */
        URL aURL = new URL("ur Image URL");
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        /* Buffered is always good for a performance plus. */
        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

        Drawable d =new BitmapDrawable(bm);
       d.setId("1");
 textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
        } catch (IOException e) {
        Log.e("error", "Remote Image Exception", e);
        } 

希望能有所帮助。

关于android - 使用 TextView.setText(Html.fromHtml(string)) 将具有 html 图像标签的字符串转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15059888/

相关文章:

android - 什么是 "SQLiteDatabase created and never closed"错误?

javascript - Android 视频元素上的 Chrome 无法从 mediaRecorder blob 数据 url 播放

android - 为什么生成的 ViewDataBinding 类将 "include"标记的属性注释为 Nullable

java - 列表中的最后一项覆盖回收站 View 项中的其他项

android - 我可以找出需要在屏幕上显示的字符串的 dp 或 px 大小吗?

android - 是否有一种属性或方法可以在对用户隐藏的 Android View 中存储值?

android - 使用阴影创建自定义 ListView

android-layout - 如何忽略子元素中的 ConstraintLayout 填充

java - Android:在 `onClick` 事件上全屏显示带有标题的图像

java - 如何隐藏或显示按钮上的元素单击下一个 Activity