【问题标题】:Unable to list parent directory from file path无法从文件路径列出父目录
【发布时间】:2020-08-27 18:57:52
【问题描述】:

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

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 文件夹包含我存储中的所有文件,

这就是我想要的。

【问题讨论】:

  • Environment.getExternalStorageDirectory().getAbsolutePath() 在大多数 Android 设备上的值为 /storage/emulated/0。所有系统都正常。
  • 但我想删除0 我不想列出它。怎么办呢
  • 你的recycleview中的Camera文件夹是/storage/emulated/0/DCIM/Camera。
  • 然后将其从列表中删除。但是您会丢失驻留在那里的视频文件。为什么它们不重要?或者不要在列表中添加“0”。易于检查
  • 您的代码不适用于 Android Q 和 R,因此是时候编写一些新的东西了。

标签: java android mediastore


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多