gpt4 book ai didi

android - Fileobserver 的 onevent 方法触发但我如何在删除任何文件之前获取文件

转载 作者:行者123 更新时间:2023-11-29 23:29:31 28 4
gpt4 key购买 nike

我想在从任何文件管理器或图库中删除图像之前复制文件,但是 FileObserveronEvent 触发,但我只想在用户删除任何文件时专门获取事件来自 FileObserver 的观察路径

observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card
@Override
public void onEvent(int event, String fileName) {
if (fileName == null) {
return;
}
//a new file or subdirectory was created under the monitored directory

if ((FileObserver.DELETE & event) != 0) {
//handle deleted file
Log.e(TAG, "onEvent: DELETE");
Log.e(TAG, "onEvent:exists(): " + new File(pathToWatch + fileName).exists());
}
}
};
observer.startWatching();

上面的代码运行良好,当用户删除任何文件时也会打印日志,但当时文件不存在并返回 false 我认为可能是文件被删除,这就是为什么它返回 false 但我想删除用于备份目的的文件。由 Dumpster 完成,请帮助我!!

最佳答案

File Observer onEvent() 方法在任何文件被删除后调用。所以你无法复制任何已删除的文件。

我知道这个解决方案不合适,但你可以试试这个来复制已删除的文件。

  1. 为您要观察的每个文件创建 fileInputStream 对象。确保该对象不会被垃圾收集。
  2. 当您在任何特定文件的 onEvent() 方法中收到删除事件时,通过使用该特定文件的文件输入流,您可以复制该文件。

    public 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();

    }

此解决方案适用于少量文件。否则你会遇到很多文件打开错误。希望对您有所帮助。

关于android - Fileobserver 的 onevent 方法触发但我如何在删除任何文件之前获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889572/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com