0

I have the follow code to get the screenshot

    View screen = getWindow().getDecorView();
    screen.setDrawingCacheEnabled(true);
    screen.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    screen.buildDrawingCache();

    Bitmap bitmap = screen.getDrawingCache();

The code is in onOptionsItemSelected in UI thread.

It runs fine when I test it manually. However, when I run the app with monkey, bitmap is nullI am not sure if it always null in monkey mode or just occasionally since monkey's randomness.

Any ideas that why monkey could behave differently? I do not want to blindly add a null pointer checker in later code.

Thanks

cheng yang
  • 1,692
  • 3
  • 12
  • 21

1 Answers1

0

you should use buildDrawingCache(true) because buildDrawingCache() is same as buildDrawingCache(false).Make sure to copy bitmap to another one before using getDrawingCache() like below.

Bitmap bt=Bitmap.createBitmap(screen.getDrawingCache());

because its copy our bitmap before recycle() if you call setDrawingCacheEnabled(false).

Pranav
  • 4,172
  • 3
  • 30
  • 31