Skip to content

Instantly share code, notes, and snippets.

@farhanfahim
Created January 21, 2020 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farhanfahim/c392a0828dc447d943dc495879426cda to your computer and use it in GitHub Desktop.
Save farhanfahim/c392a0828dc447d943dc495879426cda to your computer and use it in GitHub Desktop.
private fun getScreenShot(view: View): Bitmap {
val returnedBitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(returnedBitmap)
val bgDrawable = view.background
if (bgDrawable != null)
bgDrawable.draw(canvas)
else canvas.drawColor(Color.BLUE)
view.draw(canvas)
Toast.makeText(context, "screenshot captured", Toast.LENGTH_LONG).show()
return returnedBitmap
}
private fun saveImageInStorage(bitmap: Bitmap) {
val root: String = Environment.getExternalStorageDirectory().absolutePath
val myDir = File("$root/saved_images")
myDir.mkdirs()
var dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
var currentDateTime:String = dateFormat.format( Date())
val fileName = "$currentDateTime.jpg"
val file = File(myDir, fileName)
if (file.exists()) file.delete()
try {
val out = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
out.close()
}catch (e:Exception){
e.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment