Android message “Cannot generate texture from bitm

2020-07-16 08:24发布

I'm working with Gallery-widget and a lot of bitmaps. For erasing bitmaps I use Bitmap.recycle(). As result I see on logcat next messsage:

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

And item on Gallery-widget is black. But application works.

Have you any idea for this question?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-16 08:38

This also happens when we reuse a bitmap

Eg.bitmap=getResizedBitmap(bitmap,500); Here I have used bitmap again to resize it, which caused an issue.

查看更多
该账号已被封号
3楼-- · 2020-07-16 08:53

I just had the same problem. The thing is that by calling recycle() you destroy native object referencing to the pixel data of the image and the object itself (bitmap) is marked "dead". If there is no other object referencing to those data they will be freed by GC. So two possible thing are happening to you:

  1. the bitmaps are simply collected by GC so renderer has no data to use.
  2. as bitmap itself is marked dead you cannot read/write pixels from/to it

So if you're using/referencing this bitmap and you recycle it, you simply deny access to it for everybody. At least I understand it this way. Read this carefully:

http://developer.android.com/reference/android/graphics/Bitmap.html#recycle%28%29

So you should use recycle only if you're sure that no one is using/referencing the bitmap object. I hope it helps.

查看更多
干净又极端
4楼-- · 2020-07-16 08:59

As speedy said, you are probably trying to access bitmaps that have been marked for Garbage Collection.

A simple solution for this is to do an isRecycled() check before returning a bitmap:

// ...

/**
 *  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;
}

// ...
查看更多
登录 后发表回答