Skip to content

Instantly share code, notes, and snippets.

@hisetu
Forked from 10zgurr/SaveScreenshot.kt
Created March 19, 2022 14:29
Show Gist options
  • Save hisetu/1a3f5edda4ae8268fd1aba9c1c74bb50 to your computer and use it in GitHub Desktop.
Save hisetu/1a3f5edda4ae8268fd1aba9c1c74bb50 to your computer and use it in GitHub Desktop.
package com.theozgurr.apparchitecture.common
import android.graphics.Bitmap
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.captureToImage
import androidx.test.platform.app.InstrumentationRegistry
import java.io.FileOutputStream
fun saveScreenshot(
fileNamePrefix: String,
node: SemanticsNodeInteraction
) {
val bitmap = node
.captureToImage()
.asAndroidBitmap()
saveScreenshot(
fileName = fileNamePrefix + System.currentTimeMillis().toString(),
bitmap = bitmap
)
}
private fun saveScreenshot(
fileName: String,
bitmap: Bitmap
) {
val path = InstrumentationRegistry
.getInstrumentation()
.targetContext
.filesDir
.canonicalPath
FileOutputStream("$path/$fileName.png").use { out ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
}
println("Saved screenshot to $path/$fileName.png")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment