Android, take a screenshot

Hello everybody,

To take a screenshot you can use a simple method:

For those who do not know how to add imports 🙂
import android.graphics.Bitmap;
import android.graphics.View;
import java.io.File;
import java.io.FileOutputStream;
// set your location
private static final String SCREEN_SHOTS_LOCATION = “/media/screenshots”;

public static void takeScreenShot(View view, String name) throws Exception {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b = view.getDrawingCache();
FileOutputStream fos = null;
try {
File sddir = new File(SCREEN_SHOTS_LOCATION);
if (!sddir.exists()) {
sddir.mkdirs();
}
fos = new FileOutputStream(SCREEN_SHOTS_LOCATION + name + “_”
+ System.currentTimeMillis() + “.jpg”);
if (fos != null) {
b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
}
} catch (Exception e) {
}
}

Call:

Utils.takeScreenShot(solo.getViews().get(0), getName());

Happy screenshoting (:

Best regards,
Yahor