The Android arsenal – screenshots


Screenshot recognition library

Since version 1.0.1 there is a switch from JCenter to MavenCentral

// build.gradle (project)
allprojects { repositories { mavenCentral() /* ... */ }


implementation 'com.akexorcist:screenshot-detection:1.0.1'

This library has declared the authorization to read the external memory. So you have to manage the runtime authorization yourself. If not, the app won’t crash and screenshot detection will still work, but no file path.

Implement the library for your activity or your basic activity.

import android.os.Bundleimport com.akexorcist.screenshotdetection.ScreenshotDetectionDelegateopen class ScreenshotDetectionActivity : AppCompatActivity(), ScreenshotDetectionDelegate.ScreenshotDetectionListener { private val screenshotDetectionDelegate = ScreenshotDetectionDelegate(this, this) override fun onStart() { super.onStart() screenshotDetectionDelegate.startScreenshotDetection() } override fun onStop() { super.onStop() screenshotDetectionDelegate.stopScreenshotDetection() } override fun onScreenCaptured(path: String) { // Do something when screen was captured } override fun onScreenCapturedWithDeniedPermission() { // Do something when screen was captured but read external storage permission has denied }

However, the above example does not work because reading the external storage permission was denied. To fix this, you need to add the runtime permission request code.

import android.Manifestimport android.os.Bundleimport android.widget.Toastimport androidx.core.content.ContextCompatopen class ScreenshotDetectionActivity : AppCompatActivity(), ScreenshotDetectionDelegate.ScreenshotDetectionListener { /* ... */ companion object { private const val REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION = 3009 } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) checkReadExternalStoragePermission() } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { when (requestCode) { REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION -> { if (grantResults.getOrNull(0) == PackageManager.PERMISSION_DENIED) { showReadExternalStoragePermissionDeniedMessage() } } else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) } } private fun checkReadExternalStoragePermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestReadExternalStoragePermission() } } private fun requestReadExternalStoragePermission() { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION) } private fun showReadExternalStoragePermissionDeniedMessage() { Toast.makeText(this, "Read external storage permission has denied", Toast.LENGTH_SHORT).show() }

Then add this basic activity class to your target activity and declare it onScreenCaptured(path: String) and onScreenCapturedWithDeniedPermission() if you want to recognize the screenshot.

import android.os.Bundleimport android.widget.Toastclass MainActivity : ScreenshotDetectionActivity() { /* ... */ override fun onScreenCaptured(path: String) { Toast.make(this, path, Toast.LENGTH_SHORT).show(); // Do something when screen was captured } override fun onScreenCapturedWithDeniedPermission() { Toast.make(this, "Please grant read external storage permission for screenshot detection", Toast.LENGTH_SHORT).show() // Do something when screen was captured but read external storage permission has denied }

Copyright 2021 Akexorcist

Licensed under the Apache License, Version 2.0 (the “License”); You may only use this work in accordance with the license. You can get a copy of the license in the LICENSE file or under:

Unless required by law or agreed in writing, the software distributed under the license is distributed “AS IS” without any express or implied WARRANTIES OR CONDITIONS OF ANY KIND. In the license, you can find the specific language for permissions and restrictions under the license.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox