Как скопировать файл перед удалением с помощью FileObserver
Вот код, который я пытался скопировать, перед удалением файлов с помощью FileObserver. Пожалуйста, помогите мне решить эту проблему. Я хочу скопировать файл перед удалением в каталог приложения программно с помощью Android, например Dumpster.
public class RecoverFileObserver extends FileObserver {
private String RECOVERY_DIR = "/storage/emulated/0/Android/data/com.demo.recovery/.trash";
private String TAG = RecoverFileObserver.class.getSimpleName();
private HashMap<String, RecoverInfo> recoverInfoHashMap = new HashMap<>();
String originMd5 = "xyz";
FileInputStream fileInputStream;
String path;
String newPath;
private int length = 0;
String extension;
private HashMap<File, RecoverFileObserver> myFileObserverHashMap = new HashMap<>();
public RecoverFileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
this.path = path;
if (path.lastIndexOf(".") != -1) {
extension = path.substring(path.lastIndexOf("."));
this.newPath = RECOVERY_DIR + "/" + Md5Util.getMD5Str(path) + extension;
} else {
this.newPath = RECOVERY_DIR + "/" + Md5Util.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);
}
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.ACCESS) return;
Log.e(TAG, this.path + " | " + path + " : " + event);
switch (event) {
case FileObserver.ACCESS:
Log.e("ACCESS", "" + this.path + " | " + path + " : " + event);
break;
case FileObserver.ALL_EVENTS:
Log.e("ALL_EVENTS", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.CLOSE_NOWRITE:
Log.e("CLOSE_NOWRITE", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.CLOSE_WRITE:
Log.e("CLOSE_WRITE", "" + this.path + " | " + path + " : " + event);
break;
case FileObserver.CREATE:
Log.e("CREATE", "" + this.path + " | " + path + " : " + event);
break;
case FileObserver.MODIFY:
Log.e("MODIFY", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.MOVED_FROM:
Log.e("xyzabc", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.MOVED_TO:
Log.e("MOVED_TO", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.MOVE_SELF:
Log.e("MOVE_SELF", " " + this.path + " | " + path + " : " + event);
break;
case FileObserver.OPEN:
Log.e("OPEN", "i " + this.path + " | " + path + " : " + event);
break;
case FileObserver.ATTRIB:
Log.e("ATTRIB", " " + this.path + " | " + path + " : " + event);
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case FileObserver.DELETE:
Log.e("DELETE", " " + this.path + " | " + path + " : " + event);
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case FileObserver.DELETE_SELF:
Log.e("DELETE_SELF", "");
copyFile(fileInputStream, this.path, this.newPath, length, originMd5);
break;
case 32768:
Log.e("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;
}
}
private void copyFile(FileInputStream is, String path, String newPath, int length, String originMd5) {
try {
createOrExistsDir(new File(RECOVERY_DIR));
OutputStream os = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean createOrExistsDir(File file) {
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}
@Override
protected void finalize() {
Log.e("FINALIZE:", " finalize");
super.finalize();
}
}
Этот код для обнаружения ACCESS, OPEN, CLOSE_NOWRITE, но DELETE Event не вызывает каждый раз, какрешить эту проблему в этом коде. Спасибо!