1

If I call the method below in button clicklistener, my code works. But when i try to call this code from onCreate directly or button.performClick()

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

this line returns null pointer exception.

public void takeScreenShot(){
    try{

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        Date date = new Date();
        String d = dateFormat.format(date);
        String mPath = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/" + d + ".jpg";   
        String directory = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/";
        File imageFile2 = new File(directory);
        if(!imageFile2.isDirectory()){
            imageFile2.mkdirs();
        }
        File imageFile;
        // create bitmap screen capture
        Bitmap bitmap;
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        OutputStream fout = null;
        imageFile = new File(mPath);


        try {
            fout = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
            fout.flush();
            fout.close();

        } catch (FileNotFoundException e) {
            Log.e("MAYDAY","hata11 " + e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("MAYDAY","hata22 " + e.toString());
            e.printStackTrace();
        }
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, d + ".jpg");
        values.put(Images.Media.DESCRIPTION, d + ".jpg");
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(Images.ImageColumns.BUCKET_ID, d + ".jpg");
        values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, d + ".jpg");
        values.put("_data", imageFile.getAbsolutePath());

        ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }catch(Exception e){
        Log.e("MAYDAY","hata33 " + e.toString());
    }

}

My LogCat

Burak
  • 478
  • 1
  • 6
  • 18

1 Answers1

0

In reference to getting a snapshot of the Activity (instead of just a single View), here is one of my answers from several months ago that still applies:

I actually just did this the other day and it was a pain to get a screenshot of the entire Activity, while also making sure to remove the StatusBar from the image (as it will appear as a black rectangle in the drawing cache). This method will also allow you to overlay the returned image with some color (ie. fading)):

public static Bitmap getActivitySnapshot(Activity activity, boolean fade){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    if(fade && snapshot != null){
        Canvas canvas = new Canvas(snapshot);
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#88121212"));
        canvas.drawRect(0, 0, snapshot.getWidth(), snapshot.getHeight(), paint);
    }

    view.setDrawingCacheEnabled(false);
    return snapshot;
}   

If you don't want the fading part, the important parts would just be:

public static Bitmap getActivitySnapshot(Activity activity){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    view.setDrawingCacheEnabled(false);
    return snapshot;
}

You may also want to catch the OutOfMemoryError that may be thrown when working with large Bitmaps.

Community
  • 1
  • 1
Cruceo
  • 6,763
  • 2
  • 31
  • 52