在Android中,我们可以使用ContentObserver监听ContentProvider数据的变化。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| mPhotoObserver = new PhotoObserver(new Handler());
getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, mPhotoObserver);
class PhotoObserver extends ContentObserver { public PhotoObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { } @Override public void onChange(boolean selfChange, Uri uri) { Log.d("image onChange", selfChange + ", " + uri); Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id DESC LIMIT 1");
if (c.moveToNext()) { int imageId = c.getInt((c.getColumnIndex(MediaStore.Images.Media._ID))); String title = c.getString(c.getColumnIndex(MediaStore.Files.FileColumns.DATA)); Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); Toast toast = Toast.makeText(MainActivity.this, "刚才的IMG :" + title, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 20, 69); LinearLayout linearLayout = (LinearLayout) toast.getView(); ImageView imageView = new ImageView(MainActivity.this); imageView.setImageBitmap(bm); linearLayout.addView(imageView, 1); toast.setView(linearLayout); toast.show(); } c.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| mVideoObserver = new VideoObserver(new Handler()); getContentResolver().registerContentObserver(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true, mVideoObserver);
class VideoObserver extends ContentObserver { public VideoObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { } @Override public void onChange(boolean selfChange, Uri uri) { Log.d("video onChange", selfChange + ", " + uri); Cursor c = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id DESC LIMIT 1"); if (c.moveToNext()) { int imageId = c.getInt((c.getColumnIndex(MediaStore.Video.Media._ID))); Bitmap bm = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), imageId, MediaStore.Video.Thumbnails.MINI_KIND, null); Toast toast = Toast.makeText(MainActivity.this, "刚才的Video", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 20, 69); LinearLayout linearLayout = (LinearLayout) toast.getView(); ImageView imageView = new ImageView(MainActivity.this); imageView.setImageBitmap(bm); linearLayout.addView(imageView, 1); toast.setView(linearLayout); toast.show(); } c.close(); } }
|
我们试下拍照的事件响应:
1 2
| D/image onChange: false, content: D/image onChange: false, content:
|
当数据发生变化的时候,会通知到onChange方法。在API 16以上,可支持返回变化的Uri对象,在此之前,不会返回具体变化的对象,需要自己去找出被改变的对象。
在上面的例子中,Handler其实是基本没起到作用的,直接传null也不会影响使用。
在使用ContentObserver监听时,会接收到多次的onChange事件,事件传回来的Uri的值也有所不同,因此我们还需要对Uri做过滤的工作,以确保Uri是正确的。
当我们执行删除操作时,接收到的事件日志如下:
1
| D/image onChange: false, content://media/external/images/media
|
所以在删除时,没有返回具体的Uri对象。