如何刷新文件夹的内容?

时间:2019-07-14 07:18:34

标签: android android-studio

我正在录制屏幕并将其保存在“特定文件夹”中,然后在保存之后,我有一个按钮来显示该文件夹中的所有视频。 新保存的视频不会更新,但是如果我进入文件浏览器并打开该文件夹,则该视频将显示,然后,如果我再次在我的应用程序中看到该文件夹​​的所有视频,现在该视频也将显示在其中。如何制作该视频仅第一次出现,即保存视频后,如果单击(查看所有视频)按钮,视频应该在那里。

我的用于从文件夹中提取所有视频的代码

private void init()
{
    recyclerView = findViewById(R.id.recycler_view);
    swipeRefreshLayout = findViewById(R.id.pullToRefresh);
    layoutManager = new GridLayoutManager(getApplicationContext(),4);
    recyclerView.setLayoutManager(layoutManager);
    arrayList = new ArrayList<>();
    videoAdapter = new VideoAdapter(getApplicationContext(),arrayList,Video_Main.this);
    recyclerView.setAdapter(videoAdapter);
    fetch_videos();
}

private void fetch_videos()
{
    Uri uri;
    Cursor cursor;
    int column_index_data,thum;
    String absolutePathImage = null;

    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {
            MediaStore.MediaColumns.DATA,
            MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Video.Media._ID,
            MediaStore.Video.Thumbnails.DATA
    };
    String selection=MediaStore.Video.Media.DATA +" like?";
    String[] selectionArgs=new String[]{"%Paint App%"};
    String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    cursor = getApplicationContext().getContentResolver().query(uri,projection,selection,selectionArgs,orderBy +" DESC");
    String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);

    while(cursor.moveToNext())
    {
        absolutePathImage = cursor.getString(column_index_data);
        VideoModel videoModel = new VideoModel();

        videoModel.setBoolean_selected(false);
        videoModel.setStr_path(absolutePathImage);
        videoModel.setStr_thumbnail(cursor.getString(thum));
        arrayList.add(videoModel);
    }
    videoAdapter.setVideoList(arrayList);
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(root + "/Paint App")));
}

}

2 个答案:

答案 0 :(得分:0)

您需要在适配器中创建一个方法,

public void setVideoList(YourArray arrayList){
  this.yourList.clear();
  this.yourList.addAll(arrayList);
  notifyDataSeChanged();
}

然后删除下面几行,并将其添加到该方法的外部,但在该方法的上方,

VideoAdapter videoAdapter = new 
VideoAdapter(getApplicationContext(),arrayList,Video_Main.this);
recyclerView.setAdapter(videoAdapter);

此外,从删除线的地方从适配器调用方法,

videoAdapter.setVideoList(arrayList);

然后添加记录文件时,只需再次调用该方法fetch_videos();它将起作用。

答案 1 :(得分:0)

感谢您的帮助,我的解决方案我的sendBroadcast错误。应该是

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(file)));