Skip to content

Instantly share code, notes, and snippets.

@10zgurr
Created January 6, 2022 13:34
Show Gist options
  • Save 10zgurr/ad29e6e264893324e6c1f4403a0ca48e to your computer and use it in GitHub Desktop.
Save 10zgurr/ad29e6e264893324e6c1f4403a0ca48e 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