在SD卡指定文件夹下搜索ogg文件,列表显示,点击播放

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>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值