Monday, September 28, 2015

android - How to take ScreenShot programmatically

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp"
    android:id="@+id/rootLayout"
    tools:context=".MainActivity"
    android:background="#DCDCDC">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take ScreenShot"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:scaleType="centerCrop"
        android:src="@drawable/flower7"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.app.Activity;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;


public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the widgets reference from XML layout
                Button button = findViewById(R.id.button);
                ConstraintLayout rootLayout = findViewById(R.id.rootLayout);

                button.setOnClickListener(view -> {
                        // Take the screenshot
                        Bitmap screenShot = TakeScreenShot(rootLayout);

                        // Save the screenshot on device gallery
                        MediaStore.Images.Media.insertImage(
                                getContentResolver(),
                                screenShot,
                                "Image",
                                "Captured ScreenShot"
                        );

                        // Notify the user that screenshot taken.
                        Toast.makeText(
                                getApplicationContext(),
                                "Screen Captured.",
                                Toast.LENGTH_SHORT
                        ).show();
               });
        }


        // Custom method to take screenshot
        public Bitmap TakeScreenShot(View rootView){
                // Screenshot taken for the specified root
                // view and its child elements.
                Bitmap bitmap = Bitmap.createBitmap(
                        rootView.getWidth(),
                        rootView.getHeight(),
                        Bitmap.Config.ARGB_8888
                );
                Canvas canvas = new Canvas(bitmap);
                rootView.draw(canvas);
                return bitmap;
        }
}
AndroidManifest.xml [permission]

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