2 minutes

How to Programmatically Take a Screenshot in Android?

By Editorial @stacktips, On Sep 17, 2023 Android 2.34K Views

The following code snippet will help you to take a screenshot programmatically in Android. First you need to add the write file permission to save the captured screenshot.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Add the following Java code to capture the screenshot of running in an Activity.

private void captureScreenshot() {
    try {
        // image saving sd card path
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + System.currentTimeMillis() + ".jpg";

        // create bitmap screen capture
        View view = getWindow().getDecorView().getRootView();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        File imageFile = new File(mPath);
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    } catch (Throwable e) {
stacktips avtar


StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.