ContentObserver的onChange()方法被调用多次 [英] ContentObserver onChange() method gets called many times

查看:3954
本文介绍了ContentObserver的onChange()方法被调用多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求,跟踪设备创建类型.jpg任何新的图像文件。 我已经做到了这一点使用 ContentObserver MediaStore 使用下面的类 MediaStoreObserver 和, 注册在同我的服务之一。

我已经注意到了,的onChange()方法被调用多次为一个单一的文件创建。 我理解创建在 MediaStore 因此的onChange()被多次调用许多表得到更新的媒体文件。

我的问题:如何注册到 MediaStore 只适用于图像文件创建/编辑操作

- 谢谢预先 馒头

 私有类MediaStoreObserver扩展ContentObserver {
    公共MediaStoreObserver(){
        超(空);
    }

    @覆盖
    公共无效的onChange(布尔selfChange){
        super.onChange(selfChange);

        //检查图像文件中MediaStore变化
        readFromMediaStore(_context,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }
}

//注册图像文件的外部媒体的变化
如果(mediaStoreObserver!= NULL){
  _context.getContentResolver()。registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        假的,mediaStoreObserver);
 

解决方案

简短的回答:您不能,这是后者将提供有NotifyChange (观察员收到的onChange ),只要事情是:更新/插入/删除

长回答:这是什么会做了,实现你想要什么(如何登记MediaStore仅为图像文件创建/编辑操作?):

从MediaStore在启动时读取映像表和存储_data列(文件路径),在排序集合,排序的集合处理路径(字符串)。每当你收到的onChange 拨打的新的集合上述排序,然后遍历新的收藏和搜索您所创建的原始集合,用二进制搜索(因为收藏品排序,我们要保持的时间复杂度低)。这将导致一个非常有效的实施与运行为O(n * LOGN)的时间。

或者伪code:

  1。读取当前图像列从媒体商店(的`_data列投影)
2.将结果集,用字符串类型
3.分类收集
4.在`收到onChange`,使一个新的集合作为步骤1-3
5.遍历集合4中创建和搜索你拿出每串
在你从第3步中得到了有序集合二进制搜索,如果没有找到项目
那么该项目是新的
6.进行收集4 mediastore当前缓存版本
7.时间复杂度为O(n * log n)的对上述算法
 

修改作为更新的文件的一部分,我会读MediaStore修改日期字段,每当我在步骤5中命中的搜索,这将意味着你实际上应该存储两个文件(URI)和修改日期在数据类,但作为搜索查找中使用的文件的路径。每当找到的文件,你应该检查修改的日期相匹配,如果不是那么它是一个更新的文件。

I have requirement to track any new image file of type .jpg created on device. I have done this using ContentObserver on MediaStore using below class MediaStoreObserver and, registering the same in one of my service.

I have noticed that onChange() method gets called many times for a single file creation. I understand that media file created gets updated in many tables of MediaStore hence onChange() gets called many times.

My question: How to register to MediaStore for ONLY image file create/edit operation ?

-Thanks in advance, Manju

    private class MediaStoreObserver extends ContentObserver {
    public MediaStoreObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        //check image file changes in MediaStore
        readFromMediaStore(_context,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }
}

//register for external media changes for image files
if(mediaStoreObserver!=null){
  _context.getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        false,mediaStoreObserver);

解决方案

Short answer: You can't, it's the provider which sends notifyChange (received with observers onChange) whenever something is: updated/inserted/deleted

Longer answer: This is what would've done to achieve what you want (How to register to MediaStore for ONLY image file create/edit operation ?):

Read image table from MediaStore upon start and store the _data column (file paths), in a sorted collection, a sorted collection with paths (strings). And whenever you receive an onChange call make a new collection of the above sort, then loop over the new collection and search the original collection you created , with binary search (since the collection is sorted and we want to keep the time complexity low). This would lead to a quite effective implementation with running time of O(n*logn).

Or in pseudo code:

1. Read current image columns from media store (the `_data column as projection)
2. Store result in a collection, with string type
3. Sort collection
4. Upon `onChange` is received, make a new collection as step 1-3
5. Loop over collection created in 4 and search each string you take out with 
binary search in the sorted collection you got from step 3, if item is not found 
then the item is new
6. Make the collection in 4 the current cached version of mediastore 
7. Time complexity is O(n*log n) for the above algorithm 

Edit for the updated file part i would read the date modified field from MediaStore whenever my search in step 5 hits, that would mean that you should actually store both file (uri) and date modified in a data class, but as search lookup use file path. Whenever file is found you should check if the modified dates match, if not then it's an updated file.

这篇关于ContentObserver的onChange()方法被调用多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆