java - 无法从文件路径列出父目录
问题描述
在我的代码中,我试图收集列表中的所有视频文件并将其扭曲到父目录中并将其显示在其中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
文件夹包含我存储中的所有文件,
解决方案
根据@blackapps 评论Environment.getExternalStorageDirectory().getAbsolutePath() value on most Android devices is /storage/emulated/0
当我检查我的设备存储并且一切都很好并且文件存储在内部存储位置时
推荐阅读
- java - Android:如何检查应用程序是否正在运行
- android - 回收站视图关闭屏幕子视图持有者返回 null
- angular - 服务工作者:Angular 7 上对 API 的 HTTP GET 请求永远不会触发 fetch 事件侦听器
- python - 从numpy中的2d功率谱密度函数中检索完整的2d自相关函数
- c - C语言串口:读取非规范模式
- java - Java如何划分非常大的数字
- reactjs - 如何运行包含对静态方法的调用的快照测试?
- sql - 查询从单个(Lat,Long)中找到最近的多个(Lat,Long)
- scala - 至少对于获取的批量数据,Spark-sql Pivoting 无法按预期工作
- azure - 为什么在为 Azure SQL 创建文件 DSN 时看不到我的数据库?