0

I'm trying to keep a watch on Screenshot directory of my phone using FileObserver, I tried all events, the method onEvent id not getting called on any event. What i did is after installing the app, took one screenshot but the method didn't get called. Even tried to change the directory via a file chooser and selected camera folder and took one photo, still no action on onEvent method. This is my code: MAINACTIVITY:

package com.andi.shikharshah.fileobserver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.codekidlabs.storagechooser.StorageChooser;

import java.io.File;

public class MainActivity extends AppCompatActivity {
  FileObserverr fileObserverr;
  Button b;
  @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final StorageChooser chooser = new StorageChooser.Builder()
            .withActivity(MainActivity.this)
            .allowCustomPath(true)
            .setType(StorageChooser.DIRECTORY_CHOOSER)
            .withFragmentManager(getSupportFragmentManager())
            .withMemoryBar(true)
                .build();
        b = (Button) findViewById(R.id.b);
        b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chooser.show();
        }
    });


    chooser.setOnSelectListener(new StorageChooser.OnSelectListener() {
        @Override
        public void onSelect(String path) {
            String par = path+"/Pictures/Screenshots";
            FileObserverr obs = new FileObserverr(par);
            obs.startWatching();
        }
    });
}
} 

FILEOBSERVERCLASS:

package com.andi.shikharshah.fileobserver;

import android.os.FileObserver;
import android.support.annotation.Nullable;
import android.util.Log;

/**
* Created by Shikhar Shah on 23-05-2017.
*/

public class FileObserverr extends FileObserver
{
  String aboslutePath = "path to your directory";

  public FileObserverr(String path) {
     super(path,FileObserver.OPEN);
     aboslutePath = path;
 }

 @Override
 public void onEvent(int i, @Nullable String s) {
if(s!=null)
{
Log.d("TAG","File created");
 }
}
}
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36

1 Answers1

1

Quoting the documentation:

Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.

Your FileObserver can get garbage-collected immediately after onClick() returns. You need to hold onto it somewhere else, such as in a field of the activity.

Beyond that, you might want to look at par and ensure that the value is what you expect.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491