做个应用,需要浏览SD卡上面的所有.mp3格式的文件。
具体如下:
public class SelectMusicActivity extends Activity { private Button btnGoBack; private ListView fileListView;// 遍历文件的列表 private TextView parentPathTView;// 当前付路径的 private File parentFile;// 父路径 private ArrayList<File> currentFiles;// 当前的目录中所有的文件 private File rootFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectmusic); fileListView = (ListView) findViewById(R.id.id_fileList); parentPathTView = (TextView) findViewById(R.id.id_filePath); btnGoBack=(Button)findViewById(R.id.id_goBack); rootFile = Environment.getExternalStorageDirectory().getAbsoluteFile(); Log.i("test", rootFile.toString()); if (rootFile.exists()) { Log.i("test", "1111"); parentFile = rootFile; currentFiles = toList(rootFile.listFiles()); Log.i("test", currentFiles.size()+""); // 选择文件夹和.mp3格式的文件名字 inflateListView(currentFiles); } fileListView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (currentFiles.get(position).isFile()) {// 如果点击的是文件,直接选中 Toast.makeText(SelectMusicActivity.this, "您选择了" + currentFiles.get(position).getName() + "作为铃声", 1000).show(); } else {// 如果点击的是文件夹,进入下一集 parentFile=currentFiles.get(position); currentFiles=toList(parentFile.listFiles()); inflateListView(currentFiles); } } }); btnGoBack.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { if(!parentFile.getCanonicalPath().equals(rootFile.getCanonicalPath())){ parentFile=parentFile.getParentFile(); currentFiles=toList(parentFile.listFiles()); inflateListView(currentFiles); } } catch (IOException e) { e.printStackTrace(); } } }); } private void inflateListView(ArrayList<File> files) { List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < files.size(); i++) { Map<String, Object> fileMap = new HashMap<String, Object>(); // 文件的图标 if (files.get(i).isDirectory()) fileMap.put("icon", R.drawable.icon_package); else { fileMap.put("icon", R.drawable.icon_music); } // 文件的名字 fileMap.put("fileName", files.get(i).getName()); fileList.add(fileMap); } SimpleAdapter fileAdapter = new SimpleAdapter(this, fileList, R.layout.selectmusic_item, new String[] { "icon", "fileName" }, new int[] { R.id.id_fileIcon, R.id.id_fileName }); fileListView.setAdapter(fileAdapter); } private ArrayList<File> toList(File[] files) { ArrayList<File> arrayFiles = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { // 如果是个文件夹,或者名字是.mp3结尾,那么就方法文件ArrayList中 if (files[i].isDirectory() || files[i].toString().endsWith(".mp3")) arrayFiles.add(files[i]); } return arrayFiles; } }
selectmusci_item.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/id_fileIcon" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/id_fileName" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
selectmusic.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 显示当前目录的路径 --> <TextView android:id="@+id/id_filePath" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- 当前文件夹中文件的遍历 --> <ListView android:id="@+id/id_fileList" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- 返回按钮 --> <Button android:text="返回上层目录" android:id="@+id/id_goBack" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>