本文介绍了Android版的getPixels()可能是一个愚蠢的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是很容易理解,但我不能让它工作的一些奇怪的原因。我已经从实际code简化了这个例子。

Okay, this is quite simple to understand, but for some bizarre reason I can't get it working.. I've simplified this example from the actual code.

InputStream is = context.getResources().openRawResource(R.raw.someimage);
Bitmap bitmap = BitmapFactory.decodeStream(is);
try
{
    int[] pixels = new int[32*32];
    bitmap.getPixels(pixels, 0, 800, 0, 0, 32, 32);
}
catch(ArrayIndexOutOfBoundsException ex)
{
    Log.e("testing", "ArrayIndexOutOfBoundsException", ex);
}

为什么地球上我总是收到一个ArrayIndexOutOfBoundsException?像素阵列32×32而据我所知,我用正确的getPixels。图像尺寸是为800x800,我试图检索一个32x32的部分。图像正在被报告为ARGB-8888的32位PNG

Why on earth do I keep getting an ArrayIndexOutOfBoundsException? the pixels array is 32x32 and as far as I'm aware I'm correctly using getPixels. The image dimensions is 800x800 and I am attempting to retrieve a 32x32 section. The image is a 32-bit PNG which is being reported as ARGB-8888.

任何想法?即使我是一个傻瓜!我对扔键盘窗外:D

Any ideas? even if I'm being an idiot! I'm about to throw the keyboard out of the window :D

推荐答案

您会溢出目标缓冲区,因为你要求的800项行之间的跨度。

You're overflowing the destination buffer because you're asking for a stride of 800 entries between rows.

http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int[],%20int,%20int,%20int,%20int,%20int,%20int%29

这篇关于Android版的getPixels()可能是一个愚蠢的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 20:47