How to listen new photos in android?
H

1

7

I need to listen to new images that comes from any source like downloading images, capture new images, other apps download images..etc Is there any listener that will trigger event whenever new photos is added to gallery? I have been searching for two days. I could not get anything solid.

I read about FileObserver, will that help?

Herodotus answered 8/3, 2016 at 5:39 Comment(0)
T
13

new photos arrives to gallery

means it has been added to the MediaStore.

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.

Truong answered 12/3, 2016 at 7:3 Comment(3)
So you suggest that we should make a database as a comparison, so we can detect about addition or deletion? Isn't it too overkill? New android ContentObserver has public void onChange(boolean selfChange, Uri uri) do you think we can use that to detect addition/deletion?Convery
1.Yes , we will need our own database then. 2. No its not that much overkill. Let me explain, you should have the information about the time when did you last checked. So when you are querying , you need to update your database upto that date-time. 3. This onChange() might be useful, but however I have not used it so can't for sure. Documentation says - To ensure correct operation on older versions of the framework that did not provide a Uri argument, applications should also implement the onChange(boolean) overload of this method whenever they implement the onChange(boolean, Uri) overload.Truong
you do not need to maintain the database. You simply can use the approach used hereRambler

© 2022 - 2024 — McMap. All rights reserved.