问题描述
大家好,
很抱歉,如果你认为是重复这个问题,但我问这个问题,因为认真,我没有得到解决它。
Sorry if you think that this question is repeated but I am asking this question because seriously I am not getting the solution for it.
其实我开发一个Android应用程序中,我必须检测与在它执行的文件名,文件路径和操作的音频文件改变Android的SD卡。举例来说,如果我加入我的SD卡上的文件,然后我想知道
Actually 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. Example if I am adding a file in my sd card then I want to know
- 的文件名是添加
- 的文件的路径
- 运行 - 添加
previously我试过文件观察员但是,我必须把它在每一个目录。所以,我搜索了一些其他的解决方案,并得到了约 Audio.Media.EXTERNAL_CONTENT_URI 的信息。然后,我创建的内容观察者这样
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. Then I created a content observer like this
UriObserver.java - 这是一个内容观察者
UriObserver.java -- which is a content observer
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");
}
}
这是在code登记为它
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");
这让我知道,有些变化是发生在SD卡相关的音频文件。不过,这并不提供有关的文件已被添加,编辑或删除任何类型的信息。
It let me know that some change has been occur in sdcard related to audio files. 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
<一个href="http://stackoverflow.com/questions/12546967/android-how-to-detect-a-change-in-mediastore-when-connected-over-mtp?answertab=active#tab-top">Android:如何连接后在MTP
在这个岗位约code由 Bhiefer 作为我认为它可以工作,所以我试图执行一个答案给定的,但我不能这样做。我没有意见的特权的任何地方在我的堆栈溢出帐户所以我不能够联系*的 Bhief 的* e来问他一些帮助。
In this post some code is given by Bhiefer as an answer which I think it could work So I tried to implement but I am not able to do so. I don't have the privilege of comment anywhere in my stack-overflow account So I am not able to contact *Bhief*e for asking him for some help.
所以,任何人都可以建议我,我有什么可以做这个。如果有任何的身体有一定的解决方案,或任何人谁可以指导我如何找到解决方案了。
So anyone could suggest me what can I do for this . If any body has some solution for it or anybody who can guide me how to find solution for it.
我会很感激大家,如果你能帮助我。
I will be very grateful to you all if you can help me.
更新周三,2013年4月10日,14点47分36秒IST
如果有人知道查询Audio.Media.EXTERNAL_CONTENT_URI其最新的变化,它可以帮助的方法,但
If anyone Knows methods for querying Audio.Media.EXTERNAL_CONTENT_URI for its latest changes the it could help but
mCursor = context.getContentResolver().query(
Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id");
mCursor.moveToLast();
犯规给这么的最新更改任何其他方法,以获得最新的变化
doesnt give the latest changes So any other method to get the latest changes
推荐答案
让我尽量放松
ContentObserver
它不会给你发生了什么变化信息
It doesn't give you information on what has changed
这是每个设计。在documentation说,它会给你这个信息。
It's per design. Nothing in documentation says that it will give you this info.
FileObserver
这是不是递归的
是的。它知道问题。什么是通过所有的目录遍历和设置观察员的问题?按我的默认理解不应该有很多(我们说一打左右)。
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的:如何连接在MTP当检测MediaStore的变化
您发现只是ContentObserver包裹在UriObserver的code。
The code which you found is just ContentObserver wrapped in UriObserver.
它做一些事情
-
他得到一个游标的内容的一个提供(在他的情况下,我相信它的图像从MediaStore)
He gets a cursor for one of content provides (in his case I believe it's images from MediaStore)
他注册了一个观察员此
当一些变化发生的转发此切换到外部听众
As soon as some changes happens it forward this changes to external listener
然而,这个解决方案有两个限制:
However, this solution has two limitation:
-
它继承ContentObserver的问题,它并没有报告发生了什么数据。
It has inherit problem of ContentObserver that it doesn't report what happened to the data.
我相信它会报告只改变其注册在这个MediaStore内容提供商的文件。我相信系统扫描只在SD卡上的特殊目录,检查图像等。所以,如果一个文件会在一些地方其他目录,该解决方案将无法看到它。
I believe it will report only changes to files which are registered in this MediaStore content provider. I believe system scans only special directories on SD card to check for images and so on. So, if a file will be places in some another directory, this solution won't see it.
那么,什么是他的code你的问题?
So, What was your question about his code?
摘要
在这种情况下,如果你想知道关于scdard所有文件改变的确切类型,我不认为你可以找到比FileObserver什么都好
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
更新1
情侣更多的想法,这可能是不适合你。如果你可以用root的设备,那么你必须写筛选器驱动程序针对文件系统,所以你的驱动器将被当事情已经改变,每次叫的选项。
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.
您可以看看这个链接:http://www.gossamer-threads.com/lists/linux/kernel/355190
You can take a look at this link:http://www.gossamer-threads.com/lists/linux/kernel/355190
或者你可以重复使用一些现有的Linux更改的通知系统。作为例子,看看这个: http://stefan.buettcher.org/cs/fschange/ 。然而,这可能是因为FileObserver基于正好在它
Or you can reuse some existing linux changes notifications systems. As example, look at this:http://stefan.buettcher.org/cs/fschange/. However, it could be that FileObserver is based exactly on it.
反正,这两种方法都低的水平,将需要更多的时间来找出。
Anyway, both these approaches are low level and will require more time to figure out.
这篇关于在Android的内容观察员Audio.Media.EXTERNAL_CONTENT_URI观察变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!