golla raghu
golla raghu

Reputation: 39

How to take screen shot of current screen in android programmaticaly?

I had designed my android layout with some textviews and listview,after loading screen i just wanted to take screen shot of that layout and i have to save it on my device.Is it possible or not.

Upvotes: 0

Views: 192

Answers (2)

King of Masses
King of Masses

Reputation: 18775

From How to programatically take a screenshot on Android?

Here is the sample code:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
// 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) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

For this you need this permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 0

Vinay Jayaram
Vinay Jayaram

Reputation: 1005

Bitmap bitmap;
View v1 = findViewById(R.id.rlid);// get ur root view id
v1.setDrawingCacheEnabled(true); 
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

For saving

 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
 File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg")
 f.createNewFile();
 FileOutputStream fo = new FileOutputStream(f);
 fo.write(bytes.toByteArray()); 
 fo.close();

dont forget to give permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 1

Related Questions