简体   繁体   English

运行猴子时,getDrawingCache返回null

[英]getDrawingCache return null when run monkey

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. 该代码位于UI线程中的onOptionsItemSelected中。

It runs fine when I test it manually. 当我手动测试时,它运行良好。 However, when I run the app with monkey, bitmap is null I am not sure if it always null in monkey mode or just occasionally since monkey's randomness. 但是,当我与猴子一起运行该应用程序时, bitmap null我不确定在猴子模式下它是否总是为null,或者由于猴子的随机性而偶尔会为空。

Any ideas that why monkey could behave differently? 有什么想法为什么猴子可能会有所不同? I do not want to blindly add a null pointer checker in later code. 我不想在以后的代码中盲目添加一个空指针检查器。

Thanks 谢谢

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. 您应该使用buildDrawingCache(true),因为buildDrawingCache()与buildDrawingCache(false)相同。请确保在使用如下所示的getDrawingCache()之前将位图复制到另一个。

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

because its copy our bitmap before recycle() if you call setDrawingCacheEnabled(false). 因为如果调用setDrawingCacheEnabled(false),它会在recycle()之前复制我们的位图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM