How to Take Screenshot Programatically in Android


 // Step1: First Create the base view

               View vi = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);

                vi.setDrawingCacheEnabled(true);

  //   Step2: Create a Bitmap

                Bitmap b = vi.getDrawingCache();

//     step3: Save This bitmap







Convert Bitmap to Byte Array and Save

                             ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

                             try {
OutputStream imageFileOS = getContentResolver()
.openOutputStream(imageFileUri);
imageFileOS.write(byteArray);
imageFileOS.flush();
imageFileOS.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}