Created
July 15, 2015 06:55
-
-
Save logcat/5e9b5bbaaab2f5adbbe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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