Check if a screenshot of the screen was taken

18

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

3

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");
handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper()) {

@Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
    }
};

getContentResolver().registerContentObserver(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        true,
        new ContentObserver(handler) {
            @Override
            public boolean deliverSelfNotifications() {
                Log.d(TAG, "deliverSelfNotifications");
                return super.deliverSelfNotifications();
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
            }

            @Override
            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[] {
                                MediaStore.Images.Media.DISPLAY_NAME,
                                MediaStore.Images.Media.DATA
                        }, 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)  {
                            cursor.close();
                        }
                    }
                }
                super.onChange(selfChange, uri);
            }
        }
);

Source: Detect only screenshot with FileObserver Android

    
12.04.2017 / 22:57