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

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

问题描述

我有一个图像内容byte []表单。但是当我通过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..."

你应该转换 imageBytes 字符串到字节数组

You should convert imageBytes String to array of bytes through

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天全站免登陆