Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从文件路径列出父目录_Java_Android_Mediastore - Fatal编程技术网

Java 无法从文件路径列出父目录

Java 无法从文件路径列出父目录,java,android,mediastore,Java,Android,Mediastore,在我的代码中,我试图收集列表中的所有视频文件,并将其扭曲到父目录中,并将其显示在回收器视图中,而在显示时,我得到了一个文件夹名0,我不明白0文件夹来自何处 而0文件夹列出了我存储中的所有文件,这些文件在各自的文件夹中完全扭曲 这是文件夹zero的屏幕截图 代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV

在我的代码中,我试图收集列表中的所有视频文件,并将其扭曲到父目录中,并将其显示在
回收器视图中
,而在显示时,我得到了一个文件夹名
0
,我不明白
0
文件夹来自何处

0
文件夹列出了我存储中的所有文件,这些文件在各自的文件夹中完全扭曲

这是文件夹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)
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foldersList=新的ArrayList();
folderpath=newArrayList();
recyclerView=findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
parseAllVideo();
getUniqueFolders();
适配器=新的折页适配器(此为折页列表);
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
私有void parseAllVideo(){
试一试{
字符串[]项目={
MediaStore.Video.Media.\u 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);
做{
String filepath=videocursor.getString(路径索引);
String folder_path=MyUtils.getParentDirPath(filepath);
文件文件=新文件(文件路径);
if(file.exists()){
}
添加(文件夹路径);
}while(videocursor.moveToNext());
}
videocursor.close();
}
}捕获(例外e){
e、 printStackTrace();
}
}
私有void getUniqueFolders(){
List newList=newarraylist(newhashset(folderpath));
Collections.sort(newList);
对于(int i=0;i
这是我试图存档的内容,内部文件夹中的文件显示为缩略图(glide,bit…),但使用我的代码,
0
文件夹包含我存储中的所有文件


这是我想要的

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