getDrawingCache()总是返回null
问题描述:
我知道这个问题经常被问到,但是这似乎有所不同。我已经尝试了6种来自*的解决方案,但没有奏效。我试图在他们离开应用程序时将其绘制成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());
此代码是从*上与启用所有这些缓存。有人可以看到有什么问题吗?
顺便说一句,我当然使用Android 4.1.1。
答
问题似乎是,你不能访问surfaceview的内容 - 显然,您可以捕捉一个ImageView的内容...我假设你使用一个surfaceView因而遇到了同样的问题......如果你愿意掏进一步this如果你不使用surfaceview然后setLayerType可能是你的答案可能会帮助(从上述线程/主题之一)
...
硬件加速是否开启?在这种情况下setDrawingCacheEnabled(true);是无效的。请参阅[这里](http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled%28boolean%29)您也可以在调用启用后检查是否使用isDrawingCacheEnabled()启用了绘图缓存绘制缓存。 – 0909EM