I am trying to detect screenshots on Android app using fileObserver, fileObserver does not work on Android 6 as expected.
Here is the snippet which detects the screenshot:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screenshot_detection);
observer = new FileObserver(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_PICTURES
+ File.separator + "Screenshots" + File.separator, FileObserver.ALL_EVENTS) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.CREATE) {
Log.i(TAG, "Screenshot detected @ " + path);
}
}
};
observer.startWatching();
}
I observe that the same code works on Nexus 5 running 4.4.4 where as does not work (the onEvent is never triggered) on Nexus 5 running 6.0.1 though I have taken care of run-time permissions for API 23+.
I see a known issue with fileObserver for Android M, is there a better alternative for detecting screenshots? I tried contentObserver, faced issues with it as well.