Screenshot Capture Examples
A step by step Screenshot Capture example.
Take And Share ScreenShot
Take screenShot and share via social medeia or email , Code for kotlin code not work for emulator , you can test for real mobile.
Let us look at a full Screenshot Capture Example below.
Step 1. Our Android Manifest
We will need to look at our AndroidManifest.xml
.
(a). AndroidManifest.xml
Our
AndroidManifest
file.
Here we will add the following permission:
- Our
INTERNET
permission. - Our
WRITE_EXTERNAL_STORAGE
permission. - Our
READ_EXTERNAL_STORAGE
permission.
Here is the full Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.alialfayed.testsharescreen">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 2. Design Layouts
Let's create the following layouts:
(a). activity_main.xml
Our
activity_main
layout.
Inside your /res/layout/
directory create an xml layout file named activity_main.xml
and add the following:
androidx.constraintlayout.widget.ConstraintLayout
TextView
Button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnScreenShot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="ScreenShot"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3. Write Code
Finally we need to write our code as follows:
(a). MainActivity.kt
Our
MainActivity
class.
Create a Kotlin file named MainActivity.kt
.
We will then add imports from android SDK and other packages. Here are some of the imports we will use in this class:
Manifest
from theandroid
package.Intent
from theandroid.content
package.PackageManager
from theandroid.content.pm
package.Bitmap
from theandroid.graphics
package.Canvas
from theandroid.graphics
package.Uri
from theandroid.net
package.Bundle
from theandroid.os
package.Log
from theandroid.util
package.View
from theandroid.view
package.AppCompatActivity
from theandroidx.appcompat.app
package.ActivityCompat
from theandroidx.core.app
package.ContextCompat
from theandroidx.core.content
package.*
from thekotlinx.android.synthetic.main.activity_main
package.
Next create a class that derives from AppCompatActivity
and add its contents as follows:
We will be overriding the following functions:
onCreate(savedInstanceState: Bundle?)
.
We will be creating the following methods:
saveBitmap(parameter)
- Let's pass aBitmap
object as a parameter.screenShot(parameter)
- This function will take aView
object as a parameter.checkPermission(parameter)
- This function will take aString
object as a parameter.takePermission()
.
(a). Our takePermission()
function
Write the takePermission()
function as follows:
fun takePermission() {
val arrayPermission = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
, Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE
)
ActivityCompat.requestPermissions(this, arrayPermission, PERMISSIONCODE)
}
(b). Our screenShot()
function
Write the screenShot()
function as follows:
private fun screenShot(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
(c). Our checkPermission()
function
Write the checkPermission()
function as follows:
fun checkPermission(permission: String): Boolean {
val check: Int = ContextCompat.checkSelfPermission(this, permission)
return (check == PackageManager.PERMISSION_GRANTED)
}
(d). Our saveBitmap()
function
Write the saveBitmap()
function as follows:
private fun saveBitmap(bm: Bitmap): File {
// val path: String = Environment.getExternalStorageDirectory().absolutePath.toString() + "/Screenshots"
val path: String = this.getExternalFilesDir(null)!!.absolutePath.toString() + "/Screenshots"
val dir = File(path)
if (!dir.exists()) dir.mkdirs()
val file = File(dir, "mantis_image.png")
try {
val fOut = FileOutputStream(file)
bm.compress(Bitmap.CompressFormat.PNG, 90, fOut)
fOut.flush()
fOut.close()
} catch (e: Exception) {
e.printStackTrace()
}
return file
}
Here is the full code:
package replace_with_your_package_name
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Canvas
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.FileOutputStream
class MainActivity : AppCompatActivity() {
private val PERMISSIONCODE: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnScreenShot.setOnClickListener {
if (checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
|| checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| checkPermission(Manifest.permission.INTERNET)
) {
// val view: View = this.window.decorView
val bm: Bitmap = screenShot(this.window.decorView)
val file: File = saveBitmap(bm)
Log.i("chase", "filepath: " + file.absolutePath)
val uri: Uri = Uri.fromFile(File(file.absolutePath))
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_TEXT, "Check out my app.")
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(shareIntent, "share via"))
} else {
takePermission()
}
}
}
private fun saveBitmap(bm: Bitmap): File {
// val path: String = Environment.getExternalStorageDirectory().absolutePath.toString() + "/Screenshots"
val path: String = this.getExternalFilesDir(null)!!.absolutePath.toString() + "/Screenshots"
val dir = File(path)
if (!dir.exists()) dir.mkdirs()
val file = File(dir, "mantis_image.png")
try {
val fOut = FileOutputStream(file)
bm.compress(Bitmap.CompressFormat.PNG, 90, fOut)
fOut.flush()
fOut.close()
} catch (e: Exception) {
e.printStackTrace()
}
return file
}
private fun screenShot(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
// Check checkPermission
fun checkPermission(permission: String): Boolean {
val check: Int = ContextCompat.checkSelfPermission(this, permission)
return (check == PackageManager.PERMISSION_GRANTED)
}
// take Permission
fun takePermission() {
val arrayPermission = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
, Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE
)
ActivityCompat.requestPermissions(this, arrayPermission, PERMISSIONCODE)
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |