简体   繁体   English

观察Audio.Media.EXTERNAL_CONTENT_URI的Android内容观察者的更改

[英]Observing changes in Android content observer for Audio.Media.EXTERNAL_CONTENT_URI

I am developing an Android app in which I have to detect changes in Android SD card for audio files with the file name, file path and operation performed upon it. 我正在开发一个Android应用程序,我必须检测Android SD卡中音频文件的变化,文件名,文件路径和执行的操作。 Example if I am adding a file in my SD card then I want to know 例如,如果我在SD卡中添加文件,那么我想知道

  1. Name of the file which is added 添加的文件的名称
  2. Path of the file 文件的路径
  3. Operation -- Add 操作 - 添加

Previously I Have tried file observer But for that I have to apply it on each and every directory. 以前我已经尝试过文件观察器但是为此我必须在每个目录上应用它。 So I searched for some other solution and got the info about Audio.Media.EXTERNAL_CONTENT_URI . 所以我搜索了一些其他解决方案,并获得了有关Audio.Media.EXTERNAL_CONTENT_URI的信息。 Then I created a content observer like this 然后我创建了一个像这样的内容观察者

UriObserver.java -- which is a content observer UriObserver.java - 一个内容观察者

class UriObserver extends ContentObserver {

    public UriObserver(Handler handler) {
        super(handler);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onChange(boolean selfChange) {
        // TODO Auto-generated method stub
        super.onChange(selfChange);

        Log.d("INSTANT", "GETTING CHANGES");
    }

}

This is the code for registration for it 这是注册的代码

UriObserver observer = new UriObserver(new Handler());

Log.d("INSTANT", "registered content observer");

this.getApplicationContext()
    .getContentResolver()
    .registerContentObserver(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false,
    observer);

Log.d("INSTANT", "registered content observer");

It let me know that some change has been occur in sdcard related to audio files. 它让我知道在与音频文件相关的SD卡中发生了一些变化。 But it doesn't gives any sort of info about which file has been added, edited or deleted. 但它没有提供有关添加,编辑或删除哪个文件的任何信息。

Then I searched for for solution and got this post 然后我搜索了解决方案并得到了这篇文章

Android: How to detect a change in MediaStore when connected over MTP Android:如何通过MTP连接时检测MediaStore中的更改

In this post some code is given by Bhiefer as an answer which I think it could work, so I tried to implement that but I am not able to do so. 在这篇文章中, Bhiefer给出了一些代码作为答案,我认为它可以起作用,所以我试图实现它,但我无法这样做。

What can I do for this? 我能为此做些什么?

Update 更新

Can I query Audio.Media.EXTERNAL_CONTENT_URI for its latest changes? 我可以查询Audio.Media.EXTERNAL_CONTENT_URI的最新更改吗? This code: 这段代码:

mCursor = context.getContentResolver().query(
                    Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id");

mCursor.moveToLast();

doesn't give the latest changes. 没有给出最新的变化。 Is there any other method to get the latest changes? 有没有其他方法来获得最新的变化?

Let me try to unwind 让我试着放松一下

ContentObserver ContentObserver

It doesn't give you information on what has changed 它没有提供有关已发生变化的信息

It's per design. 这是每个设计。 Nothing in documentation says that it will give you this info. 文档中没有任何内容表明它会为您提供此信息。

FileObserver FileObserver

It isn't recursive 它不是递归的

Yes. 是。 It's know issue . 这是个问题 What is the problem with iterating through all directories and setting observers? 迭代所有目录和设置观察者有什么问题? Per my understand by default there shouldn't be many (let say a dozen or so). 根据我的理解,默认情况下不应该有很多(比方说十几个)。

Android: How to detect a change in MediaStore when connected over MTP Android:如何通过MTP连接时检测MediaStore中的更改

The code which you found is just ContentObserver wrapped in UriObserver. 您找到的代码只是包含在UriObserver中的ContentObserver。

It does several things 它做了几件事

  • He gets a cursor for one of content provides (in his case I believe it's images from MediaStore) 他得到一个内容提供的光标(在他的情况下,我相信它是来自MediaStore的图像)

  • He registers an observer for this 他为此注册了一名观察员

  • As soon as some changes happens it forward this changes to external listener 一旦发生某些变化,它就会将此更改转发给外部侦听器

However, this solution has two limitation: 但是,此解决方案有两个限制:

  • It has inherit problem of ContentObserver that it doesn't report what happened to the data. 它继承了ContentObserver的问题,它没有报告数据发生了什么。

  • I believe it will report only changes to files which are registered in this MediaStore content provider. 我相信它只会报告在此MediaStore内容提供商中注册的文件的更改。 I believe system scans only special directories on SD card to check for images and so on. 我相信系统只扫描SD卡上的特殊目录来检查图像等等。 So, if a file will be places in some another directory, this solution won't see it. 因此,如果文件将放在另一个目录中,则此解决方案将无法看到它。

So, What was your question about his code? 那么,你对他的代码有什么疑问?

Summary 摘要

In the case, if you want to know exact type of changes for ALL files on scdard, I don't think that you can find anything better than FileObserver 在这种情况下,如果你想知道scdard上所有文件的确切更改类型,我认为你不能找到比FileObserver更好的东西。

Update 1 更新1

Couple more ideas, which may be not suitable for you. 结合更多想法,这可能不适合你。 If you can root a device then you have the option to write filter driver for a filesystem, so your driver will be called each time when something has changed. 如果您可以root设备,那么您可以选择为文件系统编写过滤器驱动程序,因此每次更改某些内容时都会调用您的驱动程序。

You can take a look at this link: http://www.gossamer-threads.com/lists/linux/kernel/355190 你可以看一下这个链接: http//www.gossamer-threads.com/lists/linux/kernel/355190

Or you can reuse some existing linux changes notifications systems. 或者您可以重用一些现有的Linux更改通知系统。 As example, look at this: http://stefan.buettcher.org/cs/fschange/ . 例如,看看这个: http//stefan.buettcher.org/cs/fschange/ However, it could be that FileObserver is based exactly on it. 但是,FileObserver可能完全基于它。

Anyway, both these approaches are low level and will require more time to figure out. 无论如何,这两种方法都是低水平的,需要更多的时间来弄清楚。

You can get the latest additions/modifications by querying on the DATE_ADDED and DATE_MODIFIED columns, but you will NOT get DELETIONS . 您可以通过查询DATE_ADDED和DATE_MODIFIED列来获取最新的添加/修改, 但不会获得DELETIONS

Here is how I do it: 我是这样做的:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
long lastDatabaseUpdateTime = preferences.getLong("lastDatabaseUpdateTime", 0);
long newDatabaseUpdateTime = (new Date()).getTime();

String[] cols = new String[] {MediaStore.Audio.Media._ID /* and other columns */};

String where = "("+MediaStore.MediaColumns.DATE_ADDED + ">" + (lastDatabaseUpdateTime/1000);
where += " or " + MediaStore.MediaColumns.DATE_MODIFIED + ">" + (lastDatabaseUpdateTime/1000);
where += ") and "+MediaStore.Audio.AudioColumns.IS_MUSIC+"==1 ";

Cursor cursor = MusicUtils.query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols, where, null, null);

/* Do my work on the cursor and close the cursor */

//If no exceptions, then save the new timestamp
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("lastDatabaseUpdateTime", newDatabaseUpdateTime);
editor.commit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI报告为未知URI - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI reported as Unknown URI 在Android中的MediaStore.Audio.Media.EXTERNAL_CONTENT_URI中插入音频文件时出现问题 - problem while inserting audio file in MediaStore.Audio.Media.EXTERNAL_CONTENT_URI in android MediaStore.Audio.Media.EXTERNAL_CONTENT_URI返回一个空游标 - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI return an empty cursor ContentResolver 插入 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 错误 - ContentResolver insert MediaStore.Audio.Media.EXTERNAL_CONTENT_URI error 在查询`MediaStore.Audio.Media.EXTERNAL_CONTENT_URI`之前,有没有办法强制系统媒体重新扫描? - Is there any way to force a system media rescan before querying `MediaStore.Audio.Media.EXTERNAL_CONTENT_URI`? RecoverableSecurityException 无法访问 content://media/external/audio/media - RecoverableSecurityException no access to content://media/external/audio/media 拒绝权限:阅读com.android.providers.media.MediaProvider uri content:// media / external / images / media / 38 - Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/38 api 29+ 已弃用 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI? - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI is deprecated for api 29+? 我的设备上的查询问题(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null) - problem with query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null) on my device MediaStore.Audio.Media.EXTERNAL_CONTENT_URI不扫描数据文件夹内部 - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI does not scan inside data folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM