我正在使用Gallery-widget和许多位图。 为了擦除位图,我使用Bitmap.recycle()。结果我在logcat上看到了下一个消息:
08-18 17:49:26.020: INFO/dalvikvm-heap(13847): Grow heap (frag case) to 22.373MB for 2457616-byte allocation
08-18 17:49:26.060: DEBUG/dalvikvm(13847): GC_FOR_ALLOC freed 1K, 36% free 22835K/35143K, paused 27ms
08-18 17:49:26.230: DEBUG/dalvikvm(13847): GC_CONCURRENT freed <1K, 36% free 22835K/35143K, paused 2ms+3ms
08-18 17:49:28.070: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.080: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.110: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.120: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.140: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.160: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.180: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.190: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.210: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.230: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.240: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.260: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.280: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.290: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.310: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.330: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.350: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.360: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.380: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.400: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.410: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.430: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.450: ERROR/OpenGLRenderer(13847): Cannot generate texture from bitmap
08-18 17:49:28.500: INFO/System.out(13847): Image width: 144; height: 108
08-18 17:49:28.570: DEBUG/dalvikvm(13847): GC_FOR_ALLOC freed 4094K, 42% free 20449K/35143K, paused 27ms
08-18 17:49:28.620: INFO/dalvikvm-heap(13847): Grow heap (frag case) to 22.385MB for 2457616-byte allocation
Gallery-widget上的项目是黑色的。 但应用程序有效。
你对这个问题有任何想法吗?
答案 0 :(得分:13)
我遇到了同样的问题。问题在于,通过调用recycle(),您可以销毁引用图像像素数据的本机对象,并将对象本身(位图)标记为“死”。如果没有其他对象引用这些数据,它们将被GC释放。所以有两件事可能发生在你身上:
因此,如果您正在使用/引用此位图并进行回收,那么您只是拒绝为每个人访问它。至少我这样理解。仔细阅读:
http://developer.android.com/reference/android/graphics/Bitmap.html#recycle%28%29
因此,只有在确定没有人使用/引用位图对象时才应使用回收。我希望它有所帮助。
答案 1 :(得分:0)
作为speedy said,您可能正在尝试访问已标记为垃圾收集的位图。
一个简单的解决方案是在返回位图之前进行isRecycled()
检查:
// ...
/**
* Will return a usable Bitmap or null if the Bitmap was not found.
*/
public Bitmap getBitmapForGallery(int uniqueImageId)
{
Bitmap bitmap;
// The code to fetch bitmap from cache is here.
// ...
return bitmap.isRecycled() ? null : bitmap;
}
// ...
答案 2 :(得分:0)
当我们重用位图
时也会发生这种情况Eg.bitmap = getResizedBitmap(位图,500); 在这里,我再次使用位图来调整大小,这导致了一个问题。