How can I tell when an Album is added to the Media

2019-04-14 03:52发布

问题:

I want to run some code every time an Album is added to Android MediaStore db. I guess I need to register my app for an intent but not sure which one. Anyone know?

回答1:

Close, but you register to listen for changes to the MediaStore content URI. Something like this, for instance, probably implemented in a service:

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

This will only tell you when the MediaStore has been changed, it won't tell you what's been added or deleted. For that you'll have to query the MediaStore.

I hope that's of some use.