首页 > 解决方案 > 无法从文件路径列出父目录

问题描述

在我的代码中,我试图收集列表中的所有视频文件并将其扭曲到父目录中并将其显示在其中recycler view,并且在显示时我得到一个文件夹名称,但0我不明白该0文件夹的来源。

0文件夹列出了我存储中的所有文件,并且这些文件在它们各自的文件夹中被完美地扭曲了。

这是文件夹零的屏幕截图。

在此处输入图像描述

代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    foldersList = new ArrayList<>();
    folderpath = new ArrayList<>();

    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    parseAllVideo();
    getUniqueFolders();
    adapter = new FolderAdapter(this, foldersList);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

}

private void parseAllVideo() {
    try {
        String[] proj = {
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA};

        Cursor videocursor = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
        if (videocursor != null) {
            if (videocursor.moveToFirst()) {
                int path_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                do {
                    String filepath = videocursor.getString(path_index);
                    String folder_path = MyUtils.getParentDirPath(filepath);
                    File file = new File(filepath);
                    if (file.exists()) {

                    }
                    folderpath.add(folder_path);
                } while (videocursor.moveToNext());
            }
            videocursor.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void getUniqueFolders() {
    List<String> newList = new ArrayList<>(new HashSet<>(folderpath));
    Collections.sort(newList);
    for (int i = 0; i < newList.size(); i++) {
        String folder_name = newList.get(i).substring(newList.get(i).lastIndexOf("/") + 1);
        FolderModel folder = new FolderModel(folder_name, newList.get(i));
        Log.d(TAG, "base file = " + folder);
        foldersList.add(folder);
    }

}![enter image description here](https://i.stack.imgur.com/rgQrz.jpg)

这是我要归档的内容,内部文件夹中的文件由(滑动,位...)显示为缩略图,但是使用我的代码,该0文件夹包含我存储中的所有文件,

这就是我想要的。在此处输入图像描述

标签: javaandroidmediastore

解决方案


根据@blackapps 评论Environment.getExternalStorageDirectory().getAbsolutePath() value on most Android devices is /storage/emulated/0

当我检查我的设备存储并且一切都很好并且文件存储在内部存储位置时


推荐阅读