Skip to content

Take screenshot

View on GitHubSample viewer app

Take a screenshot of the map.

Image of take screenshot

Use case

GIS users may want to export or save a screenshot of a map to enable sharing as an image or printing.

How to use the sample

Pan and zoom to find an interesting location, then use the button to take a screenshot. The screenshot will be displayed. You can share the screenshot by tapping the share button. Or save it by tapping the save button.

How it works

  1. Wait for the MapView to finish drawing.
  2. Call exportImage() to get a BitmapDrawable.
  3. Save screenshot into a File using FileOutputStream.
  4. Share the file using context.startActivity.
  5. Save the file using context.contentResolver.

Relevant API

  • MapView

Tags

capture, export, image, print, screen capture, screenshot, share, shot

Sample Code

TakeScreenshotViewModel.ktTakeScreenshotViewModel.ktMainActivity.ktTakeScreenshotScreen.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgismaps.sample.takescreenshot.components

import android.app.Application
import android.content.ContentValues
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.content.ContextCompat.getString
import androidx.core.content.FileProvider
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import com.esri.arcgismaps.sample.takescreenshot.R
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class TakeScreenshotViewModel(app: Application) : AndroidViewModel(app) {

    val arcGISMap = ArcGISMap(BasemapStyle.ArcGISNavigationNight).apply {
        initialViewpoint = Viewpoint(39.8, -98.6, 10e7)
    }

    // Create a message dialog view model for handling error messages
    val messageDialogVM = MessageDialogViewModel()

    // screenshot image for display
    var screenshotImage: BitmapDrawable? by mutableStateOf(null)
        private set

    // class to interact with MapView
    val mapViewProxy = MapViewProxy()

    init {
        viewModelScope.launch {
            arcGISMap.load().onFailure { messageDialogVM.showMessageDialog(it) }
        }
    }

    // clears the current screenshot image by setting it to null
    fun clearScreenshotImage(){
        screenshotImage = null
    }

    // function to take screenshot of MapView
    fun takeScreenshot(){
        viewModelScope.launch {
            screenshotImage = mapViewProxy.exportImage().getOrNull()
        }
    }

    // Function to save a bitmap image to a file and return its URI
    fun saveBitmapToFile(context: Context, bitmap: Bitmap): Uri? {
        // Create a file in the cache directory
        val file = File(context.cacheDir, "take-screenshot-sample-screenshot.png")
        if (!file.exists()) {
            try {
                file.createNewFile()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        val outputStream = FileOutputStream(file)

        // Compress the bitmap and save it to the file
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        outputStream.flush()
        outputStream.close()

        // Return the URI for the file
        return FileProvider.getUriForFile(context, getString(context, R.string.take_screenshot_provider_authority), file)
    }

    fun saveBitmapToGallery(context: Context, bitmap: Bitmap): Uri? {
        val resolver = context.contentResolver
        val filename = "screenshot-${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())}.jpg"
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val imageFile = File(imagesDir, filename)
            FileOutputStream(imageFile).use { stream ->
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
            }
            // Notify the media scanner about the new file.
            MediaScannerConnection.scanFile(
                context,
                arrayOf(imageFile.toString()),
                arrayOf("image/jpeg"),
                null
            )
            return Uri.fromFile(imageFile)
        }

        // Use MediaStore API for Android 10 (API 29) and above
        val contentValues = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, filename)
            put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
            put(MediaStore.Images.Media.IS_PENDING, 1)
        }

        val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
        uri?.let {
            resolver.openOutputStream(it)?.use { stream ->
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
            }
            contentValues.clear()
            contentValues.put(MediaStore.Images.Media.IS_PENDING, 0)
            resolver.update(it, contentValues, null, null)
        }
        return uri
    }

}

// Custom FileProvider for handling file sharing of screenshots
class TakeScreenshotFileProvider : FileProvider()

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.