不知道我收到的bytearray是否可被BitmapFactory.decodeByteArray理解

时间:2012-04-19 11:46:07

标签: android bitmap

我有这个应用程序,我必须以字节数组的形式接收图像。之前,该图像已经以这种方式发送到服务器:

 image = image.createScaledBitmap((Bitmap) extras.get("data"), 380, 400, true);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        imagen.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

将图像发送到服务器。应用程序的另一部分调用服务器来获取包含图像的JSON。我将它存储在一个String中,然后我使用string.getBytes()来获取字节数组,如下所示:

byte[] array=stringimage.getbytes();

数组类似于:119,80,78,71,13,10 ...

我认为现在“好吧,我现在使用BitmapFactory,decodeByteArray并获取我的Bitmap”但它返回null。我开始googleing并在stackoverflow中查看此处,我看到了解决问题的各种方法。其中之一:

image = Bitmap.createBitmap(380, 400, Bitmap.Config.RGB_565);
int row = 0, col = 0;
for (int i = 0; i < array.length; i += 3) {
    image.setPixel(col++, row, array[i + 2] & array[i + 1] & array[i]);

    if (col == 380) {
        col = 0;
        row++;

    }
}

这就像解码图像并手动设置像素一样。

它不起作用。

另一个:

byte[] array=Base64.decode(fotostring.getBytes(),Base64.DEFAULT);

不起作用

我的问题是:我如何问我负责服务器端的伙伴给我发送阵列?以哪种格式?他没有触摸我之前以任何方式发送给他的图像。他需要这样做吗?

或者,我必须如何管理字节数组?我不需要“翻译”到另一种格式,decodeByteArray可以理解吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您面临的问题是编码/解码。从图像接收的字节数组是二进制的,不能解释为文本。为了通过基于文本的协议传输它(我假设你会使用HTTP),你必须将其编码为文本格式。从服务器接收对称操作时需要执行。

当您将其发送到服务器时,请以格式(例如Base64)进行编码,并使用相同的格式对接收到的字符串进行解码。