Open In App

How to Capture Screenshot of a View and Save it to Gallery in Android?

Last Updated : 14 Feb, 2021
Like Article

In this article, we will capture a screenshot of a view and store the image in the gallery. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Step by Step Implementation

Step 1: Create a new project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. Notice that it has a Material Card View with id card_View, we will take a screenshot of this view in this article.


<?xml version="1.0" encoding="utf-8"?>
                android:src="@drawable/gfg" />
                android:text="Geeks For Geeks"
                android:textStyle="bold" />
        android:text="Capture View"
        app:layout_constraintTop_toBottomOf="@id/card_View" />

Step 3. Add permission in the manifest.

Go to the AndroidManifest.xml file and the following code. It has two permissions Read and Write to external storage. and inside application tag it has android:usesCleartextTraffic=”true”.


<?xml version="1.0" encoding="utf-8"?>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        tools:ignore="ScopedStorage" />
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


import android.Manifest
import android.content.ContentValues
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.cardview.widget.CardView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // write permission to access the storage
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
        // this is the card view whose screenshot
          // we will take in this article
        // get the view using fin view bt id
        val cardView = findViewById<CardView>(
        // on click of this button it will capture 
          // screenshot and save into gallery
        val captureButton = findViewById<Button>(
        captureButton.setOnClickListener {
            // get the bitmap of the view using 
              // getScreenShotFromView method it is
            // implemented below
            val bitmap = getScreenShotFromView(cardView)
            // if bitmap is not null then 
              // save it to gallery
            if (bitmap != null) {
    private fun getScreenShotFromView(v: View): Bitmap? {
        // create a bitmap object
        var screenshot: Bitmap? = null
        try {
            // inflate screenshot object 
             // with Bitmap.createBitmap it 
              // requires three parameters
            // width and height of the view and
              // the background color
            screenshot = Bitmap.createBitmap(v.measuredWidth, v.measuredHeight, Bitmap.Config.ARGB_8888)
            // Now draw this bitmap on a canvas
            val canvas = Canvas(screenshot)
        } catch (e: Exception) {
            Log.e("GFG", "Failed to capture screenshot because:" + e.message)
        // return the bitmap
        return screenshot
    // this method saves the image to gallery
    private fun saveMediaToStorage(bitmap: Bitmap) {
        // Generating a file name
        val filename = "${System.currentTimeMillis()}.jpg"
        // Output stream
        var fos: OutputStream? = null
        // For devices running android >= Q
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // getting the contentResolver
            this.contentResolver?.also { resolver ->
                // Content resolver will process the contentvalues
                val contentValues = ContentValues().apply {
                    // putting file information in content values
                    put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
                    put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
                    put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
                // Inserting the contentValues to
                  // contentResolver and getting the Uri
                val imageUri: Uri? = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
                // Opening an outputstream with the Uri that we got
                fos = imageUri?.let { resolver.openOutputStream(it) }
        } else {
            // These for devices running on android < Q
            val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val image = File(imagesDir, filename)
            fos = FileOutputStream(image)
        fos?.use {
            // Finally writing the bitmap to the output stream that we opened
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
            Toast.makeText(this , "Captured View and saved to Gallery" , Toast.LENGTH_SHORT).show()


Github repo here

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads