Android - BitmapFactory.decodeByteArray 返回 null
我正在尝试使用套接字连接将图像从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这对您的情况是否有帮助,但一般来说您不应该依赖 InputStream.read(byte[], int, int) 来读取您要求的确切字节数。它只是最大值。如果您检查 您可以在 InputStream.read 文档中看到它返回您应该考虑的实际读取字节数。
通常,当从 InputStream 加载所有数据时,并期望在读取所有数据后关闭它,我会这样做。
如果您只需要加载一定量的数据,您就可以事先知道其大小。
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.
And if you have to load only certain amount of data you know the size of beforehand.