如何使用文件扩展名例如:“.mp3”或“.m4a”或“.xyz”从外部存储中列出自定义文件 - Android Studio

问题描述

我想从我的 android 设备检索文件并在列表视图或回收站视图中列出它们。我需要使用文件扩展名来执行此操作,以便我还可以检索具有自己文件扩展名的定制文件,例如:“.xyz”。 这是我到目前为止所做的

public static ArrayList<MyFiles>getAllFiles(Context context){
        ArrayList<MyFiles>tempList = new ArrayList<>();
        Uri uri = MediaStore.Files.getContentUri("external");
        Cursor cursor = context.getContentResolver().query(uri,null,null);
        if (cursor!=null && cursor.movetoFirst()){
            while (cursor.movetoNext()){
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.disPLAY_NAME));
                String ext = title.substring(title.lastIndexOf(".")+1);
                if (ext.equals("mp3")||ext.equals("m4a")||ext.equals("xyz")){
                    // check...
                    Log.e("path: "+path,"Name: "+title);
                    //adding the path and title to my Model Class
                    MyFiles myFiles = new MyFiles(path,title); 
                    tempList.add(myFiles);
                }
            }
        }
        return tempList;
    } 

方法成功返回带有“.mp3”和“.m4a”扩展名的文件列表,但未显示扩展名为“.xyz”的文件。我错过了什么,或者有更好的方法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)