public class MainActivity extends Activity {
public TextView textView;
private ListView listView;
private String pakName;
private String filePath;
private ArrayList<HashMap<String,String>> bookList;
private int index; //序列号
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
index=0;
bookList=new ArrayList<HashMap<String,String>>();
this.textView=(TextView)this.findViewById(R.id.textView);
this.pakName=textView.getText().toString();
Log.v("sdpath=",this.getSDPath()+"/"+pakName);
//检测是否有文件夹,没有创建
this.createSDCardDir();
this.filePath=this.getSDPath()+"/"+pakName;
File fileDir=new File(this.getSDPath()+"/"+pakName);
this.searchFile(fileDir);
this.listView=(ListView)this.findViewById(R.id.music_list);
//适配器
SimpleAdapter listAdapter = new SimpleAdapter(this,bookList,R.layout.sdlist,
new String[]{"number","bookName"},new int[]{R.id.id,R.id.name});
this.listView.setAdapter(listAdapter);
//监听点击事件
this.listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.v("itemId",arg2+"");
HashMap<String,String> map=(HashMap<String,String>)listView.getItemAtPosition(arg2);
String fileName=map.get("bookName");
Log.v("itemName",fileName);
//播放点击的音效
MediaPlayer player = new MediaPlayer();
String path=filePath+"/"+fileName;
try
{
player.setDataSource(path);
player.prepare();
player.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
/*
* 查找文件并加入到ArrayList中去
* @filepath 查找的目录
*/
private void searchFile(File filepath)
{
//判断SD卡状态,可读可写
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//列出filepath文件夹下所有文件和文件夹名
File[] files=filepath.listFiles();
if(files.length>0)
{
for(File file : files)
{
//判断是否是文件夹
if(file.isDirectory())
{
//如果目录可读就执行
if(file.canRead())
{
searchFile(file);//如果是目录,递归查找
}
}else if(file.getName().endsWith(".ogg"))
{
HashMap<String,String> rowItem=new HashMap<String ,String>();
rowItem.put("number", String.valueOf(index)); //加入序列号
rowItem.put("bookName", file.getName()); //加入名称
this.bookList.add(rowItem);
index++;
}
}
}
}
}
//获取SD卡路径
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED);
if (sdCardExist)
{
//获取根目录
sdDir = Environment.getExternalStorageDirectory();
}
return sdDir.toString();
}
//在SD卡下创建文件夹
public void createSDCardDir()
{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdcardDir =new File(this.getSDPath()+"/"+pakName);
if(!sdcardDir.exists())
{
sdcardDir.mkdirs();
Log.v("fileStatus","create Ok"+sdcardDir.getName());
}
}
else
{
Log.v("fileStatus","create false");
return ;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
记得添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
activity_main.xml
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pkgname"
android:textSize="30dp"
/>
<ListView
android:id="@+id/music_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
/>
</RelativeLayout>
sdlist.xml
<?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/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="25dp"
android:padding="10dp"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25dp"/>
</LinearLayout>