【问题标题】:Refresh Recycler VIew When A User Deletes/Moves File From My Apps Storage Directory? [duplicate]当用户从我的应用程序存储目录中删除/移动文件时刷新 Recyclerview? [复制]
【发布时间】: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() 中的文件夹以查看是否发生更改并相应地更新您的适配器数据集

标签: java android


【解决方案1】:

您将为音频文件创建一个ContentObserver。要么做一个服务类,要么在你的相关活动中做。

这是INTERNAL_CONTENT_URI 的实现,EXTERNAL_CONTENT_URI 也是如此。

long lastTime;
public void observeAudioFiles() {
    getContentResolver().registerContentObserver(android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true,
            new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    Log.d("your_tag", "Internal Media has been changed");
                    super.onChange(selfChange);
                    // save last time for reducing too many calls
                    Long timestamp = readLastDateFromMediaStore(LoginActivity.this, MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
                    if (timestamp > lastTime) {
                        lastTime = timestamp;
                        // TODO: New Audio Files Added, do syncing here
                    }
                }
            }
    );
}

private Long readLastDateFromMediaStore(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, "date_added DESC");
    long dateAdded = -1;
    assert cursor != null;
    if (cursor.moveToNext()) {
        dateAdded = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_ADDED));
    }
    cursor.close();
    return dateAdded;
}

我在示例中将lastTime 作为局部变量,但您将其保存在SharedPreference 中。

更新

如果你需要观察一个目录然后使用FileObserver,见@SO question

 observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card

     @Override
     public void onEvent(int event, String file) {
         //if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
         Log.d(TAG, "File created [" + pathToWatch + file + "]");

         Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
         //}
     }
 };
 observer.startWatching(); //START OBSERVING 

【讨论】:

  • 它可以跟踪单个目录吗?
  • 查看更新的答案,
  • 我尝试了以下代码,但它不起作用。它仅在我从 recyclerview 中删除文件时才有效 .. 而不是从存储中删除
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
相关资源
最近更新 更多