Check if a screenshot of the screen was taken


Snapchat has a feature that notifies the user every time someone, some other user, takes a screenshot of their stories.

At first I thought of checking to see if the user pressed Volume Down + Power , but nothing guarantees that screenshot will be saved, there may not be enough memory to save. In addition, I believe that each device can change the way of capturing the screen, not to mention that there are other applications that facilitate this process.

Another way would be to check if there has been a Screenshots directory change, using the FileObserver class , but also not there are guarantees that on all devices this path will be the same.

What would be the most feasible way to identify this action? How can I check if the user took a screenshot at the time of application usage?

asked by anonymous 03.04.2017 / 20:29

1 answer


One solution is to use ContentObserver , because a record will be inserted into the system media database after the screen screenshot.

This method requires READ_EXTERNAL_STORAGE permission.

Sample code:

HandlerThread handlerThread = new HandlerThread("content_observer");
final Handler handler = new Handler(handlerThread.getLooper()) {

    public void handleMessage(Message msg) {

        new ContentObserver(handler) {
            public boolean deliverSelfNotifications() {
                Log.d(TAG, "deliverSelfNotifications");
                return super.deliverSelfNotifications();

            public void onChange(boolean selfChange) {

            public void onChange(boolean selfChange, Uri uri) {
                Log.d(TAG, "onChange " + uri.toString());
                if (uri.toString().matches(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString() + "/[0-9]+")) {

                    Cursor cursor = null;
                    try {
                        cursor = getContentResolver().query(uri, new String[] {
                        }, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            final String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                            final String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                            // TODO: apply filter on the file name to ensure it's screen shot event
                            Log.d(TAG, "screen shot added " + fileName + " " + path);
                    } finally {
                        if (cursor != null)  {
                super.onChange(selfChange, uri);

Source: Detect only screenshot with FileObserver Android

12.04.2017 / 22:57