I want to take snapshot of my device screen's snapshot on Button click. Please provide some code sample instead of links.
Thanks in advance.
I want to take snapshot of my device screen's snapshot on Button click. Please provide some code sample instead of links.
Thanks in advance.
I got the answer for my question. Actualy i am getting bitmap as null. but i found the reason and the solution.
View v = findViewById(R.id.attachments_list);
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will
// be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
You can't take your Device's screenshot(not rooted) but within your application you can.
Below is the code which takes screenshot of your application's screen and save a file in your sdcard.
mLayoutRoot.setDrawingCacheEnabled(true); //mLayoutRoot is your Parent Layout(may be RelativeLayout, LinearLayout or etc..)
mLayoutRoot.buildDrawingCache();
Bitmap mBitmap= mLayoutRoot.getDrawingCache();
try {
if(mBitmap!=null)
{
FileOutputStream out = new FileOutputStream("/sdcard/filename.png"));
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
}
} catch (Exception e) {}