Android - BitmapFactory.decodeByteArray 返回 null

发布于 2024-11-07 05:31:21 字数 1339 浏览 7 评论 0原文

我正在尝试使用套接字连接将图像从 PC 传输到 Android。我能够从电脑接收数据到手机,但是当我将 byte[] 传递给 BitmapFactory 时,它返回 null。有时它也会返回图像,但并非总是如此。

图像大小为40054 字节。我一次接收 2048 字节,因此创建了保存 字节 数据的小型数据池(缓冲区)。收到完整数据后,我将其传递给 BitmapFactory。这是我的代码:

byte[] buffer = new byte[40054];
byte[] temp2kBuffer = new byte[2048]; 
int buffCounter = 0;
for(buffCounter = 0; buffCounter < 19; buffCounter++)
{
    inp.read(temp2kBuffer,0,2048);  // this is the input stream of socket
    for(int j = 0; j < 2048; j++)
    {
        buffer[(buffCounter*2048)+j] = temp2kBuffer[j];
    }
}
byte[] lastPacket=new byte[1142];
inp.read(lastPacket,0,1142);
buffCounter = buffCounter-1;
for(int j = 0; j < 1142; j++)
{
    buffer[(buffCounter*2048)+j] = lastPacket[j];
}
bmp=BitmapFactory.decodeByteArray(buffer,0,dataLength); // here bmp is null

计算

[19 data buffers of 2kb each] 19 X 2048 = 38912 bytes
[Last data buffer] 1142 bytes
38912 + 1142 = 40054 bytes [size of image]

我也尝试过一次读取完整的 40054 个字节,但这也不起作用。这是代码:

inp.read(buffer,0,40054);
bmp=BitmapFactory.decodeByteArray(buffer,0,dataLength); // here bmp is null

最后还用 decodeStream 检查,但结果是相同的。

知道我哪里做错了吗?

谢谢

I am trying to transfer an image from pc to android using socket connection. I am able to receive the data from pc to phone but when I pass the byte[] to BitmapFactory, it is returning null. Also sometime it is returning image but not always.

The size of image is 40054 bytes. I am receiving 2048 bytes at a time so creating small data pool (buffer) which holds the byte data. After receiving full data, I am passing it to BitmapFactory. Here is my code:

byte[] buffer = new byte[40054];
byte[] temp2kBuffer = new byte[2048]; 
int buffCounter = 0;
for(buffCounter = 0; buffCounter < 19; buffCounter++)
{
    inp.read(temp2kBuffer,0,2048);  // this is the input stream of socket
    for(int j = 0; j < 2048; j++)
    {
        buffer[(buffCounter*2048)+j] = temp2kBuffer[j];
    }
}
byte[] lastPacket=new byte[1142];
inp.read(lastPacket,0,1142);
buffCounter = buffCounter-1;
for(int j = 0; j < 1142; j++)
{
    buffer[(buffCounter*2048)+j] = lastPacket[j];
}
bmp=BitmapFactory.decodeByteArray(buffer,0,dataLength); // here bmp is null

Calculations

[19 data buffers of 2kb each] 19 X 2048 = 38912 bytes
[Last data buffer] 1142 bytes
38912 + 1142 = 40054 bytes [size of image]

I have also tried to read full 40054 bytes at a time but this also didn't worked. Here is the code:

inp.read(buffer,0,40054);
bmp=BitmapFactory.decodeByteArray(buffer,0,dataLength); // here bmp is null

Also at last checked with decodeStream but the result was same.

Any idea where I am doing wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-11-14 05:31:21

我不知道这对您的情况是否有帮助,但一般来说您不应该依赖 InputStream.read(byte[], int, int) 来读取您要求的确切字节数。它只是最大值。如果您检查 您可以在 InputStream.read 文档中看到它返回您应该考虑的实际读取字节数。

通常,当从 InputStream 加载所有数据时,并期望在读取所有数据后关闭它,我会这样做。

ByteArrayOutputStream dataBuffer = new ByteArrayOutputStream();
int readLength;
byte buffer[] = new byte[1024];
while ((readLength = is.read(buffer)) != -1) {
    dataBuffer.write(buffer, 0, readLength);
}
byte[] data = dataBuffer.toByteArray();

如果您只需要加载一定量的数据,您就可以事先知道其大小。

byte[] data = new byte[SIZE];
int readTotal = 0;
int readLength = 0;
while (readLength >= 0 && readTotal < SIZE) {
    readLength = is.read(data, readTotal, SIZE - readTotal);
    if (readLength > 0) {
        readTotal += readLength;
    }
}

I don't know if this helps in your case but in general you shouldn't rely on InputStream.read(byte[], int, int) to read the exact number of bytes you ask for. It's maximum value only. If you check InputStream.read documentation you can see that it returns actual number of bytes read you should take into account.

Usually when loading all data from InputStream, and expect it to be closed once all data is read, I do something like this.

ByteArrayOutputStream dataBuffer = new ByteArrayOutputStream();
int readLength;
byte buffer[] = new byte[1024];
while ((readLength = is.read(buffer)) != -1) {
    dataBuffer.write(buffer, 0, readLength);
}
byte[] data = dataBuffer.toByteArray();

And if you have to load only certain amount of data you know the size of beforehand.

byte[] data = new byte[SIZE];
int readTotal = 0;
int readLength = 0;
while (readLength >= 0 && readTotal < SIZE) {
    readLength = is.read(data, readTotal, SIZE - readTotal);
    if (readLength > 0) {
        readTotal += readLength;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文