从SD卡中仅读取名称中包含特定字符串的文件

时间:2019-09-10 18:10:04

标签: android glob android-storage

我编写了一个功能,可以从Android中的SD卡读取文件;现在,我只想读取名称中包含字符串'Audio.gp'的文件。

 mListView = (ListView) findViewById(R.id.listView1);
        file = Environment.getExternalStorageDirectory();
        fileNameList = getFileListfromSDCard();
        mAdapter = new FlAdapter(this, R.layout.fl_list_item, fileNameList);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if(mp!=null) {
                    try{
                        mp.stop();
                        mp.release();
                    } catch(Exception e){
                        Log.w("Exception", e);
                    }
                }
                mp = MediaPlayer.create(ListActivity.this, Uri.fromFile(fileNameList.get(position)));
                // mp = MediaPlayer.create(, file);
                try{
                    mp.prepare();
                } catch(Exception e){
                    Log.w("Exception", e);
                }
                mp.start();

由于我是Android的新手,所以我不知道该怎么做,因此,任何提示或帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您必须在AsynTask或后台线程中调用此函数。

private List<String> getListOfFilesContainString(List<String> fileNameList) {
        List<String> containFileList = new ArrayList<>();
        if (fileNameList == null && fileNameList.size() == 0) {
            return null;
        } else {
            try {
                for (int i = 0; i < fileNameList.size(); i++) {
                    File fileName = new File(Environment.getExternalStorageDirectory().getPath() + "/" + fileNameList.get(i));
                    if (!fileName.exists()) {
                        continue;
                    }
                    FileInputStream metaFileInputStream = new FileInputStream(fileName);
                    BufferedReader metaFileBufferedReader = new BufferedReader(new InputStreamReader(metaFileInputStream));
                    String strLine;
                    while ((strLine = metaFileBufferedReader.readLine()) != null) {
                        if (strLine.contains("Audio.gp")) {
                            containFileList.add(fileNameList.get(i));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return containFileList;
    }