Check if a screen screenshot has been taken

Asked

Viewed 361 times

19

The Snapchat has a feature that is to notify the user every time someone, any other user, takes a screenshot of their stories.

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

Another way would be to check if there were any directory changes Screenshots, using the class FileObserver, but there are also no guarantees that in all such devices path will be the same.

What would be the most viable way to identify this action? How can I verify if the user has taken one screenshot at the time of use of the application?

  • Ta yes @Acklay, the linked article is well explanatory and addresses very well the implementation and possibility. If this with difficulty in understanding the reference, then the problem is not in the language/functionality.

  • @juniorb2ss Are you talking about the discussion that has a link?! Here is not opening. But I will see what can be.

  • 1

    @AckLay http://stackoverflow.com/questions/29532502/detect-a-screenshot-android/29532862#29532862

  • @juniorb2ss this I can access! What is not opening is the link within this answer, here it appears that the link is broken.

  • @Acklay but not this broken, accessing normally.

  • 2

    For some reason I don’t know @juniorb2ss here is showing that the link is broken. Anyway, based on what you think is "[.. ]well explanatory and addresses very well the implementation and possibility[.. ]" you can give an answer here at Sopt, to get registered?!

  • Try to monitor the path Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots/" with the code of this reply

  • @I saw something related, maybe it’s a better option. Another option would be to capture the click event, such as button (home+volume -), but this does not guarantee that the image was saved, for example, in memory-less device situations. I’ll do some more research.

  • I’m also not sure if the path is always the one I indicated.

  • I know a way to 'block' Screenshots, but I know it’s not quite what you want. :(

  • Netflix uses this, a view to block gravações or prints.

  • @Luc is not blocking, but checking if a screenshot has been taken.

  • @acklay On the same link passed by diegofm, have a second answer. Could not with it? I don’t have the environment to test, but I was curious about the resolution of your question.

  • I think you’re complicating the solution too much. The way in various questions of Soen is to monitor the directory, if it does not serve, monitor the service that activates when taking a screenshot, if it also does not serve, then I think the only solution is to study the source code of android.

  • Will you implement a screenshot lock? a counter ? kkkkkkk dai comrade goes there you take a photo with another device and "shovel" there goes the integrity of the thing...

Show 11 more comments

1 answer

4

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

Permission is required for this method READ_EXTERNAL_STORAGE.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.