Skip to content

Instantly share code, notes, and snippets.

@logcat
Created July 15, 2015 06:55
Show Gist options
  • Save logcat/5e9b5bbaaab2f5adbbe8 to your computer and use it in GitHub Desktop.
Save logcat/5e9b5bbaaab2f5adbbe8 to your computer and use it in GitHub Desktop.
public class Screenshot {
public static final String TAG = Screenshot.class.getSimpleName();
public static void takeScreenshot(String name, Activity activity) {
String testDirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test-screenshots";
File testDir = new File(testDirPath);
testDir.mkdirs();
View scrView = activity.getWindow().getDecorView().getRootView();
scrView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(scrView.getDrawingCache());
scrView.setDrawingCacheEnabled(false);
OutputStream out = null;
File imageFile = new File(testDir, name + ".jpg");
try {
out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
} catch (IOException e) {
Log.e(TAG, "error taking screenshot", e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception exc) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment