при получении события удаления в методе onEvent () для любого конкретного файла, используя fileinputstream этого конкретного файла, вы можете скопировать файл.
открытый класс RecoverFileObserver расширяет 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();
}
}