-
-
Save hisetu/1a3f5edda4ae8268fd1aba9c1c74bb50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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