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
In hand
– user28595
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
@juniorb2ss Are you talking about the discussion that has a link?! Here is not opening. But I will see what can be.
– viana
@AckLay http://stackoverflow.com/questions/29532502/detect-a-screenshot-android/29532862#29532862
– juniorb2ss
@juniorb2ss this I can access! What is not opening is the link within this answer, here it appears that the link is broken.
– viana
@Acklay but not this broken, accessing normally.
– juniorb2ss
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?!
– viana
Try to monitor the path
Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots/"
with the code of this reply– ramaral
@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.
– viana
I’m also not sure if the path is always the one I indicated.
– ramaral
I know a way to 'block' Screenshots, but I know it’s not quite what you want. :(
– itscorey
Netflix uses this, a
view
to blockgravações
orprints
.– itscorey
@Luc is not blocking, but checking if a screenshot has been taken.
– viana
@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.
– Ismael
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.
– user28595
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...
– SneepS NinjA