I am trying to make Android app like File recovery, and for that, I am using FileObserver in my app. in my app I am Observing all the file and detecting when any file gets deleted. My app is working on some device like Motorola, and Asus. But it is not detecting in some device like Honor, Realme.
The thing is that, Same code is detecting delete operation if I am deleting the file using ES file manager app.
so my problem is fileobserver is not working in some device with its default file manager.
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();
}
}
Start Observer using line
RecoverFileObserver fileObserver = new RecoverFileObserver(videofile.get(i)); fileObserver.startWatching();