/sdcard/
을보고 싶습니다. 파일 또는 폴더 복사본이 /sdcard/
이거나 /sdcard/
인 하위 폴더에 알려지면 알려 드리겠습니다.안드로이드에있는 파일 서버
시도했지만 FileObserver
그냥 /sdcard/
에서 발생하는 복사 절차를 감지하고 하위 폴더에서 발생하는 복사 절차를 알리지 않습니다. 모든 폴더에 대해 FileObserver
을 만들어야합니까? 성능이 좋지 않습니다.
/sdcard/
을보고 싶습니다. 파일 또는 폴더 복사본이 /sdcard/
이거나 /sdcard/
인 하위 폴더에 알려지면 알려 드리겠습니다.안드로이드에있는 파일 서버
시도했지만 FileObserver
그냥 /sdcard/
에서 발생하는 복사 절차를 감지하고 하위 폴더에서 발생하는 복사 절차를 알리지 않습니다. 모든 폴더에 대해 FileObserver
을 만들어야합니까? 성능이 좋지 않습니다.
그래, 당신은 FileObserver
개체를 SD 카드의 모든 폴더에 만들어야 할 것입니다. 그리고 그것은 어떤 폴더의 onEvent()
이 불려지는지 아는 것과 폴더의 이름이 바뀌었을 때 내부 문자열을 업데이트하는 것과 같은 문제가 있습니다.
Issue 12479에 이미 문제가 제기되어 있습니다. 또한이 문제를 공개 한 사람이 작성한 패치도 있습니다. 나는 그 패치에 대해 논평 할 수는 없지만 그 패치를 시도하지는 않았다.
한 번 비슷한 상황에 직면했습니다. FileObserver Google의 Doc는 부모 폴더에 대한 Listener를 설정하면 모든 하위 폴더의 변경 사항을 알릴 수 있다고 말하지만 그렇게 작동하지는 않습니다. 일부 StackOverflowers는 opensource FileObserver와 함께 할 것을 제안 했으므로 I와 완벽하게 작동합니다. 여기에 코드가 있습니다 ..
public class RecursiveFileObserver extends FileObserver
{
public static final int CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF | MOVED_FROM | MOVED_TO;
List<SingleFileObserver> mObservers;
String mPath;
int mMask;
public String directoryPath;
public RecursiveFileObserver(String path) {
this(path, ALL_EVENTS);
directoryPath = path;
}
public RecursiveFileObserver(String path, int mask) {
super(path, mask);
mPath = path;
mMask = mask;
}
@Override
public void startWatching() {
if (mObservers != null) return;
mObservers = new ArrayList<SingleFileObserver>();
Stack<String> stack = new Stack<String>();
stack.push(mPath);
while (!stack.isEmpty()) {
String parent = stack.pop();
mObservers.add(new SingleFileObserver(parent, mMask));
File path = new File(parent);
File[] files = path.listFiles();
if (null == files) continue;
for (File f : files)
{
if (f.isDirectory() && !f.getName().equals(".") && !f.getName().equals("..")) {
stack.push(f.getPath());
}
}
}
for (SingleFileObserver sfo : mObservers) {
sfo.startWatching();
}
}
@Override
public void stopWatching() {
if (mObservers == null) return;
for (SingleFileObserver sfo : mObservers) {
sfo.stopWatching();
}
mObservers.clear();
mObservers = null;
}
@Override
public void onEvent(int event, String filePath)
{
event &= FileObserver.ALL_EVENTS;
synchronized (this)
{
//Log.i("FileManager", "event occured:"+filePath);
if (event == FileObserver.CREATE || event == FileObserver.MOVED_TO)
{
return;
}
if (event == FileObserver.DELETE || event == FileObserver.MOVED_FROM)
{
return;
}
}
}
class SingleFileObserver extends FileObserver
{
String mPath;
public SingleFileObserver(String path) {
this(path, ALL_EVENTS);
mPath = path;
}
public SingleFileObserver(String path, int mask) {
super(path, mask);
mPath = path;
}
@Override
public void onEvent(int event, String path) {
String newPath = mPath + "/" + path;
RecursiveFileObserver.this.onEvent(event, newPath);
}
}
}
재귀 함수는 스택과 많은 것들 때문에. –