读取SD卡中所有MP3文件
想做一个音乐播放器,首先当然要有播放列表啊,通过查资料,暂时先做了个获取SD卡里所有MP3文件名称的小Demo,效果图如下:
1.资源管理器预览,主要是1个Activity和2个布局文件:
2.然后就是代码了

package com.example.sdtxt; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView mListView; private ArrayList name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.mListView); name = new ArrayList(); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStorageDirectory(); // 获得SD卡路径 File[] files = path.listFiles();// 读取 getFileName(files); } SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, name, R.layout.sd_list, new String[] { "Name" }, new int[] { R.id.tc_fileName }); mListView.setAdapter(adapter); } private void getFileName(File[] files) { if (files != null)// 先判断目录是否为空,否则会报空指针 { for (File file : files) { if (file.isDirectory()) { getFileName(file.listFiles()); } else { String fileName = file.getName(); if (fileName.endsWith(".mp3")) { HashMap map = new HashMap(); String s = fileName.substring(0, fileName.lastIndexOf(".")); map.put("Name", fileName.substring(0,fileName.lastIndexOf("."))); name.add(map); } } } } } }

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_file" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="音乐文件:" /> <TextView android:id="@+id/tc_fileName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
3.最后注意添上权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
PS:修改下后缀文件名,也可用于查找其他类型文件 。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异