在SD卡指定文件夹下搜索ogg文件,列表显示,点击播放
2014-05-04 17:11
246 查看
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>
相关文章推荐
- 【已解决】C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件
- SD卡搜索文件并在textview显示,加入了looper
- 批处理实现搜索制定盘下指定文件复制到特定文件夹
- 搜索指定文件夹下的指定文件(只能用于一级目录)
- Fork/Join框架介绍III 【在一个文件夹及其子文件夹中来搜索带有指定扩展名的文件】
- 搜索项目中指定文件夹下的文件(VB)
- 安卓开发本地视频播放器——扫描本地视频文件显示在gridview上,然后点击播放。
- 【php】读取"文件列表"按时间倒序显示,并递归显示各层文件夹、!
- 获取文件夹下的文件名生成新的文件&DOC命令想从指定的txt中读出文件名,然后在指定文件夹以及子目录下搜索文件,并拷贝到指定目录
- java 从指定文件夹搜索符合条件的文件
- C# 查找指定文件夹下 指定后缀名的所有文件(文件列表)
- tomcat怎样禁止显示文件夹和文件列表
- 在指定文件夹下搜索文件
- linux中ls命令:(显示指定目录里的文件及文件夹清单)
- 遍历指定目录下的图片文件 显示在gridview 并添加点击事件
- VB得到指定文件夹下的文件列表
- Android--MediaPlayer(实现列表选歌,上一首,下一首,清空播放列表,搜索本地音乐文件)
- Android--MediaPlayer(实现列表选歌,上一首,下一首,清空播放列表,搜索本地音乐文件)
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- 从指定文件夹下获取文件路径列表的实现方法