带有位图的Android Outofmemory异常
我有一个函数,可以获取位图、两种颜色并返回位图绘制:带有位图的Android Outofmemory异常,android,out-of-memory,android-bitmap,Android,Out Of Memory,Android Bitmap,我有一个函数,可以获取位图、两种颜色并返回位图绘制: // Theme function static public BitmapDrawable pFilter(Bitmap bitmap, int backgroundColor, int foregroundColor) { Bitmap bitmapCopy = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), null, t
// Theme function
static public BitmapDrawable pFilter(Bitmap bitmap, int backgroundColor, int foregroundColor)
{
Bitmap bitmapCopy = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), null, true);
int[] pixels = new int[bitmapCopy.getByteCount()];
bitmapCopy.getPixels(pixels, 0, bitmapCopy.getWidth(), 0, 0, bitmapCopy.getWidth(), bitmapCopy.getHeight());
// Call native function
bitmapCopy.setPixels(pixels, 0, bitmapCopy.getWidth(), 0, 0, bitmapCopy.getWidth(), bitmapCopy.getHeight());
BitmapDrawable finalDrawable = new BitmapDrawable(Application.getAppContext().getResources(), bitmapCopy);
return finalDrawable;
}
// Custom Imageview
public class CustomImageView extends ImageView
{
private BitmapDrawable sourceImage;
private CustomTheme theme;
// [...]
private void refreshImageView()
{
super.setImageDrawable(theme.pFilter(sourceImage.getBitmap(), theme.backgroundColor, theme.foregroundColor));
}
我的问题是,在对该函数进行了大约80次调用(使用10px*10px位图)后,我在这一行遇到了OutOfMemory异常:
int[] pixels = new int[bitmapCopy.getByteCount()];
谢谢。打电话给pFilter(?)之后;调用bitmap.recycle();在原始位图上,错误在我的JNI调用中,而不是在Android中:
(*env)->ReleaseIntArrayElements(env, pixels, nativePixels, JNI_COMMIT);
JNI_提交:复制回内容,但不要释放elems缓冲区
解决方案是使用0而不是JNI_提交:
(*env)->ReleaseIntArrayElements(env, pixels, nativePixels, 0);
我收到一个错误:java.lang.IllegalArgumentException:无法绘制回收位图原始位图仍显示在imageView右侧?在调用recycle之前,请尝试将其替换为其他内容。是的,请查看我的代码:BitmapDrawable finalDrawable=new BitmapDrawable(Application.getAppContext().getResources(),bitmapCopy);我的主题类不知道我的ImageView。。。那么,我必须在何时何地叫唤雷克勒?在CustomImgeView的finalize?Bitmap oldBitmap=sourceImage.getBitmap()中;sourceImage=theme.pFilter(旧位图);super.setImageDrawable(theme.pFilter(旧位图、theme.backgroundColor、theme.foregroundColor));postDelayed(new Runnable(){@Override public void run(){oldBitmap.recycle();}},20);