列出媒体文件特定文件夹的图像和视频

问题描述 投票:1回答:1

请给我一个完整的示例或代码,在网格视图中列出特定文件夹中的所有媒体,包括共享和删除选项。单击导航按钮时我想要这个活动。请添加额外的图像打开和视频播放选项。

提前致谢。

java android android-fragments gallery
1个回答
1
投票

使用此类可以从特定文件夹中获取所有图像和视频

public class ListImage{
// SDCard Path
//choose your path for me i choose sdcard
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Your Folder Name/Path";
private ArrayList<ImageModel> imageList = new ArrayList<>();

// Constructor
public ListImage() {

}

public ArrayList<ImageModel> getPlayList() {

    File home = new File(MEDIA_PATH);
    if (home.listFiles(new FileExtensionFilter()) != null) {
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                ImageModel imageModel = new ImageModel();
                imageModel .setaName(file.getName().substring(0, (file.getName().length() - 4)));
                imageModel .setaPath(file.getPath());

                imageList.add(imageModel);
            }
        }
    }
    return imageList;
}
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".jpg") || name.endsWith(".JPG") || name.endsWith(".mp4") || name.endsWith(".MP4")); // add more conditions here
    }
   }
  }

ImageModel

public class ImageModel{
String aPath;
String aName;

public String getaPath() {
    return aPath;
}
public void setaPath(String aPath) {
    this.aPath = aPath;
}
public String getaName() {
    return aName;
}
public void setaName(String aName) {
    this.aName = aName;
 }

}

在您的活动/片段中

private ArrayList<ImageModel> imagesList = new ArrayList<>();

ListImage listImage=new ListImage();
this.imagesList = listImage.getPlayList();

更新:

删除文件

在你的适配器删除按钮单击

 holder.deleteButton.setOnClickListener(v -> {
   String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Your Folder Name/Path" + listImage.getaName() + ".jpg";
                            File file= new File(path);
                            if (file.exists()) {
                                if (file.delete()) {
                                    //Toast.makeText(context, "File deleted", Toast.LENGTH_SHORT).show();
                                    viewUpdate(position);
                                    fragment.loadData();
                                } else {
                                    Toast.makeText(context, "file not Deleted", Toast.LENGTH_SHORT).show();
                                }
                            }
                          }

 public void viewUpdate(int position){
    notifyItemRemoved(position);
    list.remove(position);
    notifyItemRangeChanged(position, list.size());
}

还要在清单中添加这些权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
© www.soinside.com 2019 - 2024. All rights reserved.