0

Hi I am creating an application that would detect if a screenshot has been taken and then retrieve that particular screenshot. So I have been able to detect if screenshots are taken.

But I am wondering how would I be able to get that screenshot that was just detected?

This is my Main Activity:

public static File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
public static File f = new File(sdDir, "Screenshots/");
public static String paths = f.getPath();

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = getApplicationContext();

   directoryFileObserver = new DirectoryFileObserver(paths);
    directoryFileObserver.startWatching();
}

and this is my File Observer class:

public class DirectoryFileObserver extends FileObserver {

public DirectoryFileObserver(String path) {
    super(path,FileObserver.CREATE);
    paths = path;
}
@Override
public void onEvent(int event, String path) {
    if(path != null){
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("SCREENSHOT DETECTED")
                        .setContentText("YOU CREATED A NEW SCREENSHOT");

        // Add as notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}
}

Thanks in advance for any suggestions to point me in the right direction or help! :D

  • "So I have been able to detect if screenshots are taken" -- your code is unlikely to be reliable across device models. There is no requirement that screenshots be stored where you are looking. – CommonsWare Dec 15 '17 at 19:30
  • @CommonsWare Oh okay sir, my initial thought was that when a phone stores a screenshot it would go under the `/storage/emulated/0/Pictures/Screenshots` folder so I just monitored that specific folder. hehe. Sir may I ask what could I look into? –  Dec 15 '17 at 19:40
  • 1
    "my initial thought was that when a phone stores a screenshot it would go under..." -- there are ~10,000 Android device models, spread over 27 OS versions. The behavior of any given device will depend on the OS version and manufacturer changes. There is no documented official screenshot storage location that manufacturers must use. "what could I look into?" -- take screenshots yourself using the media projection APIs on Android 5.0+. Or, ask the user to choose an image to use (e.g., `ACTION_PICK`). – CommonsWare Dec 15 '17 at 20:02
  • @CommonsWare well that kinda sucks, but thanks alot sir for the explanation it really gave me an insight on what could be done. Yes sir, I actually checked your git on Media Projection and I guess I will use that. Thanks for the help sir! :D –  Dec 15 '17 at 20:08

0 Answers0