【问题标题】:Base64 image not displayed in android imageviewBase64图像未显示在android imageview中
【发布时间】:2019-01-30 18:27:31
【问题描述】:

我试图在图像视图中显示 base64 图像,但它不显示。我正在解码和设置图像视图的图像位图。 我正在动态添加图像视图。问题是因为我动态添加图像吗? 下面是代码sn-p:

LayoutInflater inflaterDocuments = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View inflatedHeaderLayout = inflaterDocuments.Inflate(Resource.Layout.imageListItem, null, false);
ImageView imageView = inflatedHeaderLayout.FindViewById<ImageView>(Resource.Id.imageView);
byte[] decodedString = Base64.Decode(base64String, Base64Flags.Default);
Bitmap bitMap = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);
imageView.SetImageBitmap(bitMap);
imageView.Invalidate();
_imageLayout.AddView(inflatedHeaderLayout);

编辑

Base64 字符串文件Base64.txt

【问题讨论】:

  • 是否有任何解决方案适合您?

标签: android xamarin.android imageview base64


【解决方案1】:

这是获取base64的方法

public static String getBase64String(Bitmap image) {
    String encodeString = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        encodeString = Base64.encodeToString(byteArray, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encodeString;
}

幻灯片用于展示图片

String photoId = base64;
    Glide.with(getApplicationContext())
            .load(photoId)
            .apply(RequestOptions.circleCropTransform())
            .into(employeeImage);

【讨论】:

  • 什么是 Glide?我在 Xamarin android 中做。
  • implementation 'com.github.bumptech.glide:glide:4.8.0' 将此添加到您的 gradle 文件中。这是一种管理图像视图的简单方法。
  • 我必须在哪里将其设置为 imageview?什么是employeeImage 是imageview?
  • 添加这是 oncreate ImageView employeeImage = findViewById(R.id.iv_image);
  • 如果不起作用,请在线检查您的 base64 图像。我认为您解决了问题
【解决方案2】:

创建将Base64转换为Bitmap的方法

public Bitmap BaseStringToBitmap(string imageBase64)
{
    byte[] imageBytes = Convert.FromBase64String(imageBase64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

比在ImageView对象上使用它

imageView.SetImageBitmap(BaseStringToBitmap(base64String));

示例代码

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.activity_main);
    var iv = FindViewById<Android.Widget.ImageView>(Resource.Id.image1);
    iv.SetImageBitmap(BaseStringToBitmap());
}   
public Bitmap BaseStringToBitmap()
{
    var bse64 = "/9j/4ZT8RXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAgAAAA...";
    byte[] imageBytes = Convert.FromBase64String(bse64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

Axml 文件

<ImageView
android:id="@+id/image1"
android:background="#89CFF0"    
android:layout_width="200dp" 
android:layout_height="200dp"/>

输出截图

【讨论】:

  • @Arti - 请您在文本文件或其他方式中提供base64 字符串,以便我检查。
  • 我无法上传 base64 卡住了。
  • 但是如果我将base64放在浏览器中,它会打开。
  • 您可以使用此站点textuploader.com 公开您的base64 网址
  • 没有帮助,即使 textuploader 卡住了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2015-11-09
  • 2019-06-25
  • 2013-01-31
  • 2020-02-04
  • 1970-01-01
相关资源
最近更新 更多