Q: How to programatically take a screenshot on Android?

D: How can I take screenshot of selected area of phone-screen not by any program, but from code.

Test Case #21


File ID: #5651242-0-cc


public void captureImage() {
//captures screen image
    String Path = Environment.getExternalStorageDirectory().toString() + "/"; //+ ACCUWX.IMAGE_APPEND;
    Bitmap bitmap;
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    OutputStream out = null;
    File imageFile = new File(Path);
    try {
        out = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  1. Hi, Can you describe what is mCurrentUrlMask? I have tried this code but it is always giving me NullPointerException at Bitmap.createBitmap(v1.getDrawingCache()), Can anybody tell what I am doing wrong.? Any help is appreciated. Thanks.
  2. Insted of `View v1 = mCurrentUrlMask.getRootView();` I have used `View v1 = getWindow().getDecorView().getRootView();` and it works for me.
  3. This just takes a screenshot of your own program (right?)

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: