我知道這個問題經常被問到,但是這似乎有所不同。我已經嘗試了6種來自StackOverflow的解決方案,但沒有奏效。我試圖在他們離開應用程序時將其繪製成Notification.BigPictureStyle
。getDrawingCache()總是返回null
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
_thread.setRunning(true);
_thread.start();
setDrawingCacheEnabled(true);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
_thread.setRunning(false);
measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
buildDrawingCache(false);
Bitmap currentState = Bitmap.createBitmap(getDrawingCache());
setDrawingCacheEnabled(false);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(DrawActivity.this, DrawActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(DrawActivity.this, 0, notificationIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification noti = new Notification.BigPictureStyle(
new Notification.Builder(DrawActivity.this)
.setContentTitle("Your drawing")
.setContentText("Unfold to see drawing")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent))
.setSummaryText("Here you can see how you left your drawing.")
.bigPicture(currentState)
.build();
manager.notify(1023, noti);
}
}
但我始終得到了NullPointerException
這行:
Bitmap currentState = Bitmap.createBitmap(getDrawingCache());
此代碼是從StackOverflow上與啓用所有這些緩存。有人可以看到有什麼問題嗎?
順便說一句,我當然使用Android 4.1.1。
硬件加速是否開啓?在這種情況下setDrawingCacheEnabled(true);是無效的。請參閱[這裏](http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled%28boolean%29)您也可以在調用啓用後檢查是否使用isDrawingCacheEnabled()啓用了繪圖緩存繪製緩存。 – 0909EM