在Android中截取屏幕截图时出错

时间:2013-04-01 17:55:27

标签: android screenshot

我正在尝试拍摄我正在绘制的图表的屏幕截图,但每次尝试我都会得到NullExceptionPointer

这是我的代码:

lineChart.setChartData(array1,array2,xd);
View v1 = lineChart.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

其中lineChart是自定义视图

此logcat将此行指示为null:bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 这是我的logcat:

04-01 19:21:11.524: E/AndroidRuntime(333): FATAL EXCEPTION: main

04-01 19:21:11.524: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appui/com.example.appui.CompareActivity}: java.lang.NullPointerException
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-01 19:21:11.524: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:21:11.524: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 19:21:11.524: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-01 19:21:11.524: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.graphics.Bitmap.createBitmap(Bitmap.java:367)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.example.appui.CompareActivity.onCreate(CompareActivity.java:37)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-01 19:21:11.524: E/AndroidRuntime(333):  ... 11 more

2 个答案:

答案 0 :(得分:0)

我没有使用过getRootView,但是the documentation说明了它

  

返回包含此视图的最顶层视图

如果它本身是最顶层的View,它将返回null。尝试

View v1 = (View)lineChart;

代替。

the documentation(和here),我猜你可以强制通过添加

来构建绘图缓存
    // this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
   v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
   v1.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
   v1.buildDrawingCache();
在使用

获取位图之前

bitmap = Bitmap.createBitmap(v1.getDrawingCache());

答案 1 :(得分:0)

View.getDrawingCache(boolean)州的文档:

  

返回缓存此视图图形的位图。 返回   禁用缓存时,位图为空。

使用setDrawingCacheEnabled(boolean)将绘图缓存设置为true。您还可以使用isDrawingCacheEnabled()检查是否启用了绘图缓存。