【编程语言】Android--如何遍历android所有路径

这篇博客介绍了如何在Android应用中遍历SD卡,查找并列出所有的MP3和WAV音乐文件。通过File对象遍历目录,检查每个文件的后缀名,将匹配的文件路径添加到列表中,然后使用SimpleAdapter展示在ListView上。当没有找到音乐文件时,显示提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

list = (ListView) findViewById(R.id.music_music_list);

                file = new File("/sdcard/");
                it = new ArrayList<String>();
                searchFiles(file);
                SimpleAdapter ladapter = new SimpleAdapter(this,getLData(),R.layout.music_item, new String[]{"NAME"},new int[]{R.id.music_list_item});
                list.setAdapter(ladapter);
                TextView tv = new TextView(this);
        tv.setText("sd卡中没有音乐文件!");
        tv.setPadding(0, 50, 0, 0);
        addContentView(tv, new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
        list.setEmptyView(tv);

 

        private void searchFiles(File f) {   //搜索sdcard中的所有MP3文件
                // TODO Auto-generated method stub
                File files[] = f.listFiles();
                for(File tempF:files){
                        if(tempF.isDirectory()){
                                searchFiles(tempF);
                        }
                        else{
                                String path = tempF.getPath();
                                String fpath = path.substring(path.lastIndexOf(".")+1,path.length());
                                if(fpath.equals("mp3")||fpath.equals("wav")){
                                        it.add(path);
                                }
                        }
                }
        }


        private List<? extends Map<String, ?>> getLData() {
                // TODO Auto-generated method stub
                ArrayList<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
                HashMap<String,Object> item;
                int i = 0 ;
                for(i=0;i<it.size();i++){
                        item = new HashMap<String,Object>();
                        String path  = it.get(i).toString();
                        String name = path.substring(path.lastIndexOf("/")+1,path.length());
                        item.put("NAME",name);
                        System.out.println("data"+it.get(i).toString());
                        data.add(item);
                }
                return data;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值