Created
January 21, 2020 12:39
-
-
Save farhanfahim/c392a0828dc447d943dc495879426cda 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
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