我正在尝试使Android应用像文件恢复一样,为此,我在应用中使用FileObserver。在我的应用程序中,我正在观察所有文件并检测何时删除任何文件。我的应用程序正在像Motorola和Asus这样的设备上运行。但这并未在Honor,Realme等设备中检测到。
问题是,如果我使用ES文件管理器应用程序删除文件,则相同的代码正在检测删除操作。
所以我的问题是fileobserver在使用默认文件管理器的某些设备中无法正常工作。
class RecoverFileObserver extends FileObserver {
private final String TAG = RecoverFileObserver.class.getSimpleName();
String originMd5="xyz";
FileInputStream fileInputStream;
String path;
String newPath;
private int length = 0;
String extension;
public RecoverFileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
this.path = path;
Log.d(TAG, "patException:" + path);
if (path.lastIndexOf(".") != -1) {
extension = path.substring(path.lastIndexOf("."));
this.newPath = RECOVERY_DIR + "/" + MD5Utils.getMD5Str(path) + extension;
} else {
this.newPath = RECOVERY_DIR + "/" + MD5Utils.getMD5Str(path);
}
try {
fileInputStream = new FileInputStream(path);
length = fileInputStream.available();
} catch (IOException e) {
e.printStackTrace();
}
RecoverInfo info = new RecoverInfo();
info.originMd5 = originMd5;
info.recoveryPath = newPath;
recoverInfoHashMap.put(path, info);
Log.e(TAG, "actualpath:" + path + "\n orignalmd5:" + originMd5);
}
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.ACCESS) return;
Log.v(TAG, this.path + " | " + path + " : " + event);
switch (event) {
case FileObserver.ACCESS:
Log.d("xyzabc", "inside Access");
break;
case FileObserver.ALL_EVENTS:
Log.d("xyzabc", "inside AllEvents");
break;
case FileObserver.CLOSE_NOWRITE:
Log.d("xyzabc", "inside CLOSE_NOWRITE");
break;
case FileObserver.CLOSE_WRITE:
Log.d("xyzabc", "inside CLOSE_WRITE");
break;
case FileObserver.CREATE:
Log.d("xyzabc", "inside CREATE");
break;
case FileObserver.MODIFY:
Log.d("xyzabc", "inside MODIFY");
break;
case FileObserver.MOVED_FROM:
Log.d("xyzabc", "inside MOVED_FROM");
break;
case FileObserver.MOVED_TO:
Log.d("xyzabc", "inside MOVED_TO");
break;
case FileObserver.MOVE_SELF:
Log.d("xyzabc", "inside MOVE_SELF");
break;
case FileObserver.OPEN:
Log.d("xyzabc", "inside OPEN");
break;
case FileObserver.ATTRIB:
Log.d("xyzabc", "inside attrib");
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case FileObserver.DELETE:
Log.d("xyzabc", "inside delete");
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case FileObserver.DELETE_SELF:
Log.d("xyzabc", "inside delete self");
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case 32768:
Log.d("xyzabc", "inside 32768");
stopWatching();
File file = new File(this.path);
if (file.exists()) {
RecoverFileObserver fileObserver = new RecoverFileObserver(this.path);
fileObserver.startWatching();
myFileObserverHashMap.put(file, fileObserver);
} else {
myFileObserverHashMap.remove(file);
}
break;
default:
break;
}
}
@Override
protected void finalize() {
Log.d("xyzabc", "inside finalize");
super.finalize();
}
}
使用行启动观察者
RecoverFileObserver fileObserver =新的RecoverFileObserver(videofile.get(i)); fileObserver.startWatching();