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

2019-04-14 03:25发布

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条回答
ら.Afraid
2楼-- · 2019-04-14 04:05

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.

查看更多
登录 后发表回答