mSnapshotLayout is getting main layout id whose snapshot you want, Like this
mSnapshotLayout = (LinearLayout) findViewById(R.id.snapshotLayout);
Use This inside your button click
View v1 = mSnapshotLayout.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
storeImage(bm);
And Here is the method to store that Image
@SuppressLint("SimpleDateFormat")
private boolean storeImage(Bitmap imageData) {
// get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory()
+ "/Snapshot/";
File sdIconStorageDir = new File(iconsStoragePath);
// create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
String filename = "Deal" + currentDateandTime + ".png";
try {
File file = new File(sdIconStorageDir.toString() + File.separator
+ filename);
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
imageData.compress(CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
MediaScannerConnection.scanFile(this,
new String[] { file.getPath() },
new String[] { "image/jpeg" }, null);
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}