1

I am making an audio recorder app, when i delete the recording file from the directory manually and when i open the app its should remove the deleted file from the Recycler List view any answer how to do this?

enter image description here

public class ObserveFiles extends FileObserver { public RecyclerViewAdapter recyclerViewAdapter;

public String absolutePath;

public ObserveFiles(String path) { super(path, FileObserver.ALL_EVENTS);

absolutePath = path;

}

..............................................................................

@Override public void onEvent(int event, @Nullable String path) {

if (path == null) {
    return;
}
//a new file or subdirectory was created under the monitored directory
if ((FileObserver.DELETE & event)!=0) {
    Log.d("Deleted---------->", "File Deleted [" + absolutePath +  "/" + path + "]");
    String filepath = absolutePath + "/" + path;
    recyclerViewAdapter.removeOutOfApp(filepath);
}

//data was written to a file
if ((FileObserver.MODIFY & event)!=0) {
    Log.d("Deleted---------->", "File Modified [" + absolutePath  +  "/" + path + "]");
}

//the monitored file or directory was deleted, monitoring effectively stops
if ((FileObserver.DELETE_SELF & event)!=0) {
    Log.d("Deleted---------->", "File Self Deleted [" + absolutePath + "/URecorder" +  "/" + path + "]");
}

//a file or directory was opened
if ((FileObserver.MOVED_TO & event)!=0) {
    Log.d("Deleted---------->", "File Moved To [" +  absolutePath  +  "/" + path  + "]");
}

//a file or subdirectory was moved from the monitored directory
if ((FileObserver.MOVED_FROM & event)!=0) {
    Log.d("Deleted---------->", "File Moved From [" + absolutePath  +  "/" + path + "]");
}

//the monitored file or directory was moved; monitoring continues
if ((FileObserver.MOVE_SELF & event)!=0) {
    Log.d("Deleted---------->", "File Moved Self[" + absolutePath  + "/" +  path + "]");
}

}

Alex Boro
  • 31
  • 2

1 Answers1

1

You will create a ContentObserver for audio files. Either make a service class, or do it in your related Activity.

Here is an implementation for INTERNAL_CONTENT_URI, also do same for EXTERNAL_CONTENT_URI.

long lastTime;
public void observeAudioFiles() {
    getContentResolver().registerContentObserver(android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true,
            new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    Log.d("your_tag", "Internal Media has been changed");
                    super.onChange(selfChange);
                    // save last time for reducing too many calls
                    Long timestamp = readLastDateFromMediaStore(LoginActivity.this, MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
                    if (timestamp > lastTime) {
                        lastTime = timestamp;
                        // TODO: New Audio Files Added, do syncing here
                    }
                }
            }
    );
}

private Long readLastDateFromMediaStore(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, "date_added DESC");
    long dateAdded = -1;
    assert cursor != null;
    if (cursor.moveToNext()) {
        dateAdded = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_ADDED));
    }
    cursor.close();
    return dateAdded;
}

I put lastTime as local variable in sample, but you save it in SharedPreference.

Update

If you need to observe a directory then use FileObserver, see @SO question

 observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card

     @Override
     public void onEvent(int event, String file) {
         //if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
         Log.d(TAG, "File created [" + pathToWatch + file + "]");

         Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
         //}
     }
 };
 observer.startWatching(); //START OBSERVING 
Community
  • 1
  • 1
Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182