I am making an application in Android in which I have to take screenshot of one of my activities and mail it as attachment.
I want to take screenshot of the current
Kotlin complete solution Code with permission check :
1- Use this nice library to take screen shot with java / Kotlin / Rx functionality, add library dependency : InstaCapture github link
implementation "com.github.tarek360:instacapture:2.0.1"
2- Must check permission for compatibility on all android versions:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) { // Needs permission so request it
DeviceUtil.showAlertMsg(this, GeneralDicModel.shareMsgScreenShot!!)
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
) //callback result to onRequestPermissionsResult
} else { //Has got the permission before or doesn't need
screenShotAndShareIt()
}
3- Check permission result :
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array,
grantResults: IntArray
) {
when (requestCode) {
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
screenShotAndShareIt()
} else {
// toast("Permission must be granted in order to save scrrenshot file")
}
}
}
}
4- Func call for take Screenshot & Share it by intent :
fun screenShotAndShareIt() {
Instacapture.capture(this, object : SimpleScreenCapturingListener() {
override fun onCaptureComplete(bitmap: Bitmap) {
val state = Environment.getExternalStorageState()
if (Environment.MEDIA_MOUNTED == state) {
val path: String = Environment.getExternalStorageDirectory().toString()
val picDir = File(path.plus("/myPic"))
if (!picDir.exists()) {
picDir.mkdir()
}
var bitmapScreenShot = bitmap
val fileName = "screenshot" + ".jpg"
val picFile = File(picDir.path.plus("/" + fileName))
try {
picFile.createNewFile()
val picOut = FileOutputStream(picFile)
bitmapScreenShot =
Bitmap.createBitmap(bitmapScreenShot, 0, 0, bitmapScreenShot.width, bitmapScreenShot.height)
val saved: Boolean = bitmapScreenShot.compress(Bitmap.CompressFormat.JPEG, 100, picOut)
if (saved) {
Log.i(
TAG,
"ScreenShotAndShareIt : Image saved to your device Pictures " + "directory! + ${picFile.absolutePath}"
)
} else {
Log.i(TAG, "ScreenShotAndShareIt Error on Save! + ${picFile.absolutePath}")
}
picOut.close()
// share via intent
val intent: Intent = Intent(android.content.Intent.ACTION_SEND)
intent.type = "image/jpeg"
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picFile.absolutePath))
startActivity(Intent.createChooser(intent, "Sharing"))
} catch (e: Exception) {
Log.i(TAG, "ScreenShotAndShareIt Error catch : " + e.printStackTrace())
}
} else {
//Error
Log.i(TAG, "ScreenShotAndShareIt Error Environment.MEDIA_MOUNTED == state : " )
}
}
})
5- Declare this variable :
val PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 100
6- Do not forget to Add this permissions to AndroidManifest.xml: