0

I have this function in my MainActivity.kt file which I want to produce a screenshot of the google map being rendered, then display it in an image view and save it. This is related to what I am trying to do but I don't have enough experience with Kotlin, Java and android studio to understand what isn't working.

My main issue is that I can't figure out how to get / access a variable containing the Bitmap

This is being added to the code from This Google Tutorial

private fun takePicture(googleMap: GoogleMap) {
    var bitmapfrommap: Bitmap? = null
    val snapshotReadyCallback : GoogleMap.SnapshotReadyCallback = GoogleMap.SnapshotReadyCallback {
        fun onSnapshotReady(snapshot: Bitmap) {
            bitmapfrommap = snapshot
            imageView.setImageBitmap(bitmapfrommap)
            var filename = "export.png"
            var path = getExternalFilesDir(null)
            var fileOut = File(path, filename)
            if (bitmapfrommap != null) {
                fileOut.writeBitmap(bitmapfrommap!!, Bitmap.CompressFormat.PNG, 85)
            }
        }
    }
    val onMapLoadedCallback : GoogleMap.OnMapLoadedCallback = GoogleMap.OnMapLoadedCallback {
        googleMap.snapshot(snapshotReadyCallback, bitmapfrommap)
    }
    googleMap.setOnMapLoadedCallback(onMapLoadedCallback)
}
JayBigGuy
  • 1
  • 1

1 Answers1

1
binding.takeMapScreenshot.setOnClickListener {
            myMap?.snapshot { bitmap -> //here you do whatever you want with the resulting bitmap }
}