【发布时间】:2023-03-31 11:20:01
【问题描述】:
我正在制作一个录音机应用程序,当我手动从目录中删除录音文件时,当我打开应用程序时,它应该从回收站列表中删除已删除的文件查看任何答案如何做到这一点?
公共类 ObserveFiles 扩展 FileObserver { public RecyclerViewAdapter recyclerViewAdapter;
公共字符串绝对路径;
public ObserveFiles(字符串路径){ 超级(路径,FileObserver.ALL_EVENTS);
absolutePath = path;
}
.................................................. ....................
@覆盖 public void onEvent(int event, @Nullable String path) {
if (path == null) {
return;
}
//a new file or subdirectory was created under the monitored directory
if ((FileObserver.DELETE & event)!=0) {
Log.d("Deleted---------->", "File Deleted [" + absolutePath + "/" + path + "]");
String filepath = absolutePath + "/" + path;
recyclerViewAdapter.removeOutOfApp(filepath);
}
//data was written to a file
if ((FileObserver.MODIFY & event)!=0) {
Log.d("Deleted---------->", "File Modified [" + absolutePath + "/" + path + "]");
}
//the monitored file or directory was deleted, monitoring effectively stops
if ((FileObserver.DELETE_SELF & event)!=0) {
Log.d("Deleted---------->", "File Self Deleted [" + absolutePath + "/URecorder" + "/" + path + "]");
}
//a file or directory was opened
if ((FileObserver.MOVED_TO & event)!=0) {
Log.d("Deleted---------->", "File Moved To [" + absolutePath + "/" + path + "]");
}
//a file or subdirectory was moved from the monitored directory
if ((FileObserver.MOVED_FROM & event)!=0) {
Log.d("Deleted---------->", "File Moved From [" + absolutePath + "/" + path + "]");
}
//the monitored file or directory was moved; monitoring continues
if ((FileObserver.MOVE_SELF & event)!=0) {
Log.d("Deleted---------->", "File Moved Self[" + absolutePath + "/" + path + "]");
}
}
【问题讨论】:
-
有一个图片链接你可以看到我在说什么
-
您应该扫描 onResume() 中的文件夹以查看是否发生更改并相应地更新您的适配器数据集