Skip to content
Advertisement

Can’t use FileObserver to Observe Files Inside a Folder

I want to observe if any file/folder is added inside /storage/emulated/0/DCIM/Camera using FileObserver in a service, but it didn’t work. Please tell me if there is anything wrong in my code 🙏. Here is the source code:

AndroidManifest.xml

I added the uses-permission and service tags:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dtautosender">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service android:name=".LogWatcherService"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LogWatcherService.java

I added FileObserver inside the service, and overrided its onEvent method:

package com.example.dtautosender;

import android.app.Service;
import android.content.Intent;
import android.os.FileObserver;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.io.File;

public class LogWatcherService extends Service {
    private static final String TAG = "LogWatcherService";
    public static FileObserver fo;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final File directoryToWatch = new File(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/");

        fo = new FileObserver(directoryToWatch.getAbsolutePath(), FileObserver.ALL_EVENTS) {
            @Override
            public void onEvent(int event, @Nullable String path) {
                Log.d(TAG, "onEvent: " + path);
            }
        };
        fo.startWatching();

        Toast.makeText(this, "Log Watcher Service is started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStartCommand: Log Watcher Service is started and trying to watch: " + directoryToWatch);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Log Watcher Service is stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy: Log Watcher Service is stopped");

        fo.stopWatching();
    }
}

MainActivity.java

I start the service when opening the app when storage permission is granted:

package com.example.dtautosender;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

    private static final String TAG = "MainActivity";
    private boolean watcherIsEnabled = false;
    private Context ctx;

    private Button toggleWatcherButton;
    private TextView statusTextView;

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

        ctx = this;

        statusTextView = findViewById(R.id.status_text);
        toggleWatcherButton = findViewById(R.id.watcher_toggle);

        addEventListeners();
        requestPermissions();
    }

    private void addEventListeners() {
        toggleWatcherButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLogWatcherService();
            }
        });
    }

    private void requestPermissions() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                Log.d(TAG, "requestPermissions: Permission isn't granted");
            } else {
                ActivityCompat.requestPermissions(
                        this,
                        new String[] {
                                Manifest.permission.READ_EXTERNAL_STORAGE
                        },
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
        } else {
            Log.d(TAG, "startLogWatcherService: Permission has already been granted");
            startLogWatcherService();
        }
    }

    private void startLogWatcherService() {
        if (!watcherIsEnabled) {
            startService(new Intent(ctx, LogWatcherService.class));
            statusTextView.setText("Status: Active");
            statusTextView.setTextColor(Color.parseColor("#00FF00"));
            toggleWatcherButton.setText("Stop Service");
        } else {
            stopService(new Intent(ctx, LogWatcherService.class));
            statusTextView.setText("Status: Non-Active");
            statusTextView.setTextColor(Color.parseColor("#FF0000"));
            toggleWatcherButton.setText("Start Service");
        }
        watcherIsEnabled = !watcherIsEnabled;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startLogWatcherService();
                } else {
                    toggleWatcherButton.setText("Watch file permission isn't granted by the user");
                    toggleWatcherButton.setEnabled(false);
                }
                return;
            }
        }
    }
}

I started the application and there is onStartCommand on the log. But the onEvent method is never called when changes happen.

Advertisement

Answer

I solved this problem by myself. I think the problem is because my Android Studio API level is 30, which “Environment.getExternalStorageDirectory()” is deprecated. So, to solve that I added the following attribute to the application tag in AndroidManifest.xml

android:requestLegacyExternalStorage="true"

Now, my FileObserver is working as expected

Advertisement