Skip to content

Instantly share code, notes, and snippets.

@nilandev
Last active August 29, 2015 14:14
Show Gist options
  • Save nilandev/efb10633cccf4b75dc30 to your computer and use it in GitHub Desktop.
Save nilandev/efb10633cccf4b75dc30 to your computer and use it in GitHub Desktop.
Taking Screenshot of current screen and saves on external storage
private void captureScreen() {
View content = findViewById(R.id.content_frame);
Bitmap bitmap = content.getDrawingCache();
File folder = new File(Environment.getExternalStorageDirectory()+ "/Device Info");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
final String fileName = new SimpleDateFormat("'/'yyyyMMddhhmmss'.png'").format(new Date());
final File file = new File(folder.getAbsoluteFile() + fileName );
//file observer
new FileObserver(folder.getAbsolutePath()) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.CREATE)
HomeScreenActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Screenshot saved to :" +file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
}
});
}
}.startWatching();
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Do something else on failure
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment