无法从文件路径列出父目录
Unable to list parent directory from file path
在我的代码中,我试图收集列表中的所有视频文件并将其扭曲到父目录中并在 recycler view
中显示,并且在显示时我得到一个文件夹名称 0
我不知道不明白 0
文件夹的来源。
并且 0
文件夹列出了我存储中的所有文件,并且这些文件在各自的文件夹中完全扭曲。
and here is a screen shot of the folder zero.
代码
@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
当我检查我的设备存储时,一切正常,文件存储在内部存储位置
在我的代码中,我试图收集列表中的所有视频文件并将其扭曲到父目录中并在 recycler view
中显示,并且在显示时我得到一个文件夹名称 0
我不知道不明白 0
文件夹的来源。
并且 0
文件夹列出了我存储中的所有文件,并且这些文件在各自的文件夹中完全扭曲。
and here is a screen shot of the folder zero.
代码
@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
当我检查我的设备存储时,一切正常,文件存储在内部存储位置