我编写了一个功能,可以从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的新手,所以我不知道该怎么做,因此,任何提示或帮助将不胜感激!
答案 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;
}