首页 > 解决方案 > 自定义 html 视图以支持 android textview 中的图像。Web 视图在 base64 图像中加载非常慢

问题描述

我想使用 nativescript 的 htmlview 来渲染 base64encoded 图像。有没有办法自定义htmlview,因为有很多文章支持android textview中的图像,而nativescript htmlview被包裹在android textview上

我试过,使用通过android的fromHtml方法插入base64图像的方法,但没有一个有效。它显示 [obj] 而不是图像

getSafe(text: string, view: any) { let context = view._context;

let d = new android.text.Html.ImageGetter({
  // @Override
  getDrawable(source: string): android.graphics.drawable.Drawable {
    let base_64_source: string = source.replace(/data.*base64/g, "");
    const data = android.util.Base64.decode(
      base_64_source,
      android.util.Base64.DEFAULT
    );
    const bitmap: android.graphics.Bitmap = android.graphics.BitmapFactory.decodeByteArray(
      data,
      0,
      data.length
    );
    let image: android.graphics.drawable.Drawable = new android.graphics.drawable.BitmapDrawable(
      context.getResources(),
      bitmap
    );
    image.setBounds(
      0,
      0,
      0 + image.getIntrinsicWidth(),
      0 + image.getIntrinsicHeight()
    );

    return image;

    // return new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);
  }
});

图像显示为 [OBJ] 而不是图像

标签: androidnativescript

解决方案


推荐阅读