如何使用 Glide 通过字节数组加载图像? [英] How to load image through byte array using Glide?

查看:110
本文介绍了如何使用 Glide 通过字节数组加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像内容字节 [] 形式.但是当我通过 Glide 加载它们时,会显示损坏的图像.我正在做的如下所示.

I have an image contents byte[] form. But when i load them through Glide then broken images are shown. what I'm doing is shown below.

Glide.with(context)
    .load(imageByteArray)
    .asBitmap()
    .placeholder(R.drawable.ic_broken)
    .into(rowImageView);

imageByteArray 在不使用滑行的情况下成功转换为位图.所以图像字节数组没有错.

imageByteArray successfully converts to bitmap without using glide. So there is no wrong with image byte array.

请指导我缺少什么?

我也在使用 Glide 库 com.github.bumptech.glide:glide:3.6.1以及 Android 支持库 com.android.support:support-v13:23.0.1

Also I'm using Glide library com.github.bumptech.glide:glide:3.6.1 And Android support library com.android.support:support-v13:23.0.1

已编辑

好的,这就是我正在做的.

Ok, This is what I'm doing.

String imageBytes = "HVao14fpmtHSev3OgsrQNsawkFzXNcY3BsfQla6..."

上面定义的这个字符串是我从 Web API 接收到的实际图像的字节数.

This string above defined is bytes of actual image which I'm receiving from web API.

我正在将这个字符串转换成这样的字节数组

I'm converting this String into byte array like this

public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
}

我将生成的字节数组imageByteArray"应用于上面定义的 Glide 代码.

Than I'm applying resultant byte array "imageByteArray" to Glide code defined above.

推荐答案

假设你的 base64 字符串是

Lets say your base64 string is

String imageBytes = "HVao14fpmtHSev3OgsrQNsawkFzXNcY3BsfQla6..."

您应该通过

byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);

然后将此imageByteArray 传递给 Glide.

afterwards pass this imageByteArray to Glide.

Glide.with(context)
    .load(imageByteArray)
    .asBitmap()
    .placeholder(R.drawable.ic_broken)
    .into(rowImageView);

这篇关于如何使用 Glide 通过字节数组加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆