0

I am trying to register a FileObserver inside of a service to monitor whenever a new photo has been saved to "/DCIM/Camera/". So far I have tried everything I could find online. In my opinion it should already work...

i do request permissions in the manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <service android:name=".FileWatcherService"
             android:enabled="true"
             android:exported="false"/>
    <!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
    <receiver android:name=".StartupReceiver" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

my observer:

public class FileWatcherService extends Service {

static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
private static String TAG = "FileWatcher";

public static FileObserver fileObserver = new FileObserver(PATH, CREATE) {
    @Override
    public void onEvent(int i, @Nullable String s) {
        Log.d(TAG, "eventttttt"); //never triggered
    }
};

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    Log.d(TAG, "onDestroy: service destroyed");
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate: service created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int res = super.onStartCommand(intent, flags, startId);

    fileObserver.startWatching();

    Log.d(TAG,"Service started!");
    return Service.START_STICKY;
}

public static void start(Context ctx) {
    Intent i = new Intent(ctx, FileWatcherService.class);
    ctx.startService(i);
}
}

The main activity:

public class MainActivity extends AppCompatActivity {

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(".","Permission: "+permissions[0]+ "was "+grantResults[0]);

        FileWatcherService.start(getApplicationContext());
    }
}


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

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}

What am I missing? The service starts just fine.

Thanks for your help.

darude_cod3r
  • 91
  • 3
  • 11

1 Answers1

0

For locally confirming your changes, go to Settings->App info->Your app->Permissions and enable 'Storage' permissions. Close your app completely (remove from tasks too) and then check if your code works.

What looks to be actually missing is requesting user for access to external storage.

Reference: https://developer.android.com/training/permissions/requesting

Bharath Kumar
  • 541
  • 3
  • 10
  • I did provide and grant write permissions using the permission system. However I did update my question and added the main activity. Maybe that helps. – darude_cod3r Aug 08 '18 at 21:44