![](/img/trans.png)
[英]When anyDensity=false why does getDrawingCache(true) return null?
[英]getDrawingCache return null when run monkey
我有以下代碼來獲取屏幕截圖
View screen = getWindow().getDecorView();
screen.setDrawingCacheEnabled(true);
screen.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
screen.buildDrawingCache();
Bitmap bitmap = screen.getDrawingCache();
該代碼位於UI線程中的onOptionsItemSelected
中。
當我手動測試時,它運行良好。 但是,當我與猴子一起運行該應用程序時, bitmap
null
我不確定在猴子模式下它是否總是為null,或者由於猴子的隨機性而偶爾會為空。
有什么想法為什么猴子可能會有所不同? 我不想在以后的代碼中盲目添加一個空指針檢查器。
謝謝
您應該使用buildDrawingCache(true),因為buildDrawingCache()與buildDrawingCache(false)相同。請確保在使用如下所示的getDrawingCache()之前將位圖復制到另一個。
Bitmap bt=Bitmap.createBitmap(screen.getDrawingCache());
因為如果調用setDrawingCacheEnabled(false),它會在recycle()之前復制我們的位圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.