1

我知道这个问题经常被问到,但是这似乎有所不同。我已经尝试了6种来自StackOverflow的解决方案,但没有奏效。我试图在他们离开应用程序时将其绘制成Notification.BigPictureStylegetDrawingCache()总是返回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。

+2

硬件加速是否开启?在这种情况下setDrawingCacheEnabled(true);是无效的。请参阅[这里](http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled%28boolean%29)您也可以在调用启用后检查是否使用isDrawingCacheEnabled()启用了绘图缓存绘制缓存。 – 0909EM

回答

0

类似问题here更多信息here ...

问题似乎是,你不能访问surfaceview的内容 - 显然,您可以捕捉一个ImageView的内容...我假设你使用一个surfaceView因而遇到了同样的问题......如果你愿意掏进一步this如果你不使用surfaceview然后setLayerType可能是你的答案可能会帮助(从上述线程/主题之一)

...

+0

你说得对。唉..那我应该怎么实现呢? – tolgap

+0

您的绘图代码对于surfaceView是什么样的?也许将画布绘制成位图?让我知道如果你仍然卡住...看到你的绘制代码(或其中的一部分)可能会帮助... – 0909EM

+0

我已经放弃了它。 SurfaceView的限制太多。现在寻找其他方法。 – tolgap