NullPointerException when taking screenshot

时间:2015-09-01 22:36:38

标签: android

I'm trying to take screenshot, but I'm getting error

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

on line

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

Main.java:

public class Main extends Activity {

     @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                File file = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES), "Screenshots");

                String date = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

                try {
                    View view = getWindow().getDecorView().getRootView();
                    view.setDrawingCacheEnabled(true);
                    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                    view.setDrawingCacheEnabled(false);

                    File imgFile = new File(file.getPath() + File.separator +
                            "IMG_"+ date + ".jpg");

                    FileOutputStream outputStream = new FileOutputStream(imgFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
}

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main">

</RelativeLayout>

Why is this happening and how to fix this?

2 个答案:

答案 0 :(得分:3)

This is likely happening because you are drawing your screenshot in your Activity#onCreate(). At this point, your View has not measured its dimensions, so View#getDrawingCache() will return null because width and height of the view will be 0.

You can move your screenshot code away from onCreate() or you could use a ViewTreeObserver.OnGlobalLayoutListener to listen for when the view is about to be drawn.

Only after View#getWidth() returns a non-zero integer can you get your screenshot.

答案 1 :(得分:0)

从@ugo的建议中得到解决方案

将其放在您的onCreate函数中

    @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate( savedInstanceState );
setContentView( R.layout.activity_share );
    //
    ...
    ///
            myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                      //take a screenshot
                      screenShot();
                    }}