RuntimeException的:没有缓冲区像素足够大 [英] RuntimeException: Buffer not large enough for pixels

查看:3268
本文介绍了RuntimeException的:没有缓冲区像素足够大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到过插座字节数组位图,我读了它,然后我想将它设置 os.toByteArray ImageView的在我的应用程序。在code我用的是:

I'm receiving a Bitmap in byte array through socket and I read it and then I want to set it os.toByteArray as ImageView in my application. The code I use is:

try {
    //bmp = BitmapFactory.decodeByteArray(result, 0, result.length);
    bitmap_tmp = Bitmap.createBitmap(540, 719, Bitmap.Config.ARGB_8888);
    ByteBuffer buffer = ByteBuffer.wrap(os.toByteArray());

    bitmap_tmp.copyPixelsFromBuffer(buffer);
    Log.d("Server",result+"Length:"+result.length);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            imageView.setImageBitmap(bitmap_tmp);
        }
    });
    return bmp;
} finally {
}

当我运行我的应用程序,并开始接受字节[] 并期望的ImageView 被改变,事实并非如此。

When I run my application and start receiving Byte[] and expect that ImageView is changed, it's not.

LogCat中说:

java.lang.RuntimeException: Buffer not large enough for pixels at
android.graphics.Bitmap.copyPixelsFromBuffer

我在寻找类似的问题,但无法找到解决我的问题。

I searched in similar questions but couldn't find a solution to my problem.

推荐答案

看看源(版本2.3.4_r1,最后一次是位图前4.4更新grep的code)在<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.4_r1/android/graphics/Bitmap.java#Bitmap.copyPixelsFromBuffer%28java.nio.Buffer%29"相对=nofollow>位图:: copyPixelsFromBuffer()

Take a look at the source (version 2.3.4_r1, last time Bitmap was updated on Grepcode prior to 4.4) for Bitmap::copyPixelsFromBuffer()

错误的措辞有点不清楚,但code clarifies--就意味着你的缓存的计算公式为没有足够的数据来填充你的位图的像素。 这是(可能)因为它们使用缓冲器的剩余的()方法来计算的缓冲器,其中考虑到其位置属性的当前值的容量。如果调用快退()在您的缓冲区调用copyFromPixels(前),你可能会看到运行时异常消失。我说'可能',因为ByteBuffer的::包()方法应该设置位置属性值为零,无需调用快退,而是通过类似的问题和我自己的经验复位位置明确可以做的伎俩判断。

The wording of the error is a bit unclear, but the code clarifies-- it means that your buffer is calculated as not having enough data to fill the pixels of your bitmap. This is (possibly) because they use the buffer's remaining() method to figure the capacity of the buffer, which takes into account the current value of its position attribute. If you call rewind() on your buffer before you invoke copyFromPixels(), you might see the runtime exception disappear. I say 'might' because the ByteBuffer::wrap() method should set the position attribute value to zero, removing the need to call rewind, but judging by similar questions and my own experience resetting the position explicitly may do the trick.

尝试

ByteBuffer buffer = ByteBuffer.wrap(os.toByteArray());
buffer.rewind();
bitmap_tmp.copyPixelsFromBuffer(buffer);

这篇关于RuntimeException的:没有缓冲区像素足够大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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