3

What is the best option (MediaScanner/FileObserver) to monitor couple of paths (both internal and external memory of device) for media files being created.

I need to get a event when ever a media file is being created in folder that is being monitored.

-Thanks & regards, Manju

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
Manju
  • 677
  • 5
  • 13
  • 27
  • Hi did you find any solution to solve the issue? – M. Reza Nasirloo Jul 09 '14 at 22:06
  • Hi Pedram, It is MediaScanner ! This is bcoz you get complete Media info in this and only need to filter for which you are intereseted. This can be achieved by setting filter in your query. – Manju Jul 16 '14 at 07:23

2 Answers2

1

First of all, FileOberver is a memory-killer approach. Consider a high volume of files. Rather ContentObserver seems a far better approach.

getContentResolver().registerContentObserver(android.provider.MediaStore.Images.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);
                Long timestamp = readLastDateFromMediaStore(context, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                // comapare with your stored last value and do what you need to do

            }
        }
    );
getContentResolver().registerContentObserver(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, 
    new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            Log.d("your_tag","External Media has been changed");
            super.onChange(selfChange);

            Long timestamp = readLastDateFromMediaStore(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            // comapare with your stored last value and do what you need to do
        }
    }
);

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

Probably a good idea to do this in a service (ever running)! You will also need to unregister in the onDestroy()

Warning: This only tells you when the MediaStore has been changed, it does not tellly anything specific about addition/deletion. For this, you may have to query the MediaStore to detect any change from your previous database or something.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
0

The best solution in your case is use a BroadcastReceiver. Read more about it on url above.

TN888
  • 7,659
  • 9
  • 48
  • 84
  • For the broadcast receiver, you need to add an intent filter, action in the manifest file. So in the above case what would you have it as? – Zax Aug 16 '13 at 08:27
  • 2
    How to get notified when any media file being created/moved in device internal/external memory on any path (not only on path dcim/camera but on any path in both internal/external memory of device) – Manju Aug 17 '13 at 10:16
  • @Ty221 which broadcast receiver intent will notify content provider media change – Ankesh kumar Jaisansaria Apr 06 '16 at 05:33