我有以下代码来获取屏幕截图
View screen = getWindow().getDecorView();
screen.setDrawingCacheEnabled(true);
screen.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
screen.buildDrawingCache();
Bitmap bitmap = screen.getDrawingCache();
代码位于UI线程中的onOptionsItemSelected
。
我手动测试时运行正常。但是,当我使用猴子运行应用程序时,bitmap
为null
我不确定它是否在猴子模式下始终为空,或者只是偶尔因为猴子的随机性。
为什么猴子会表现出不同的想法?我不想在以后的代码中盲目添加空指针检查器。
谢谢
答案 0 :(得分:0)
你应该使用buildDrawingCache(true),因为buildDrawingCache()与buildDrawingCache(false)相同。在使用getDrawingCache()之前,请确保将位图复制到另一个位图。
Bitmap bt=Bitmap.createBitmap(screen.getDrawingCache());
因为如果你调用setDrawingCacheEnabled(false),它会在recycle()之前复制我们的位图。