如何从SD卡上所有的android的所有音频文件

如何从SD卡上所有的android的所有音频文件

本文介绍了如何从SD卡上所有的android的所有音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一类,它获得所有MP3文件从整个SD卡。但实际上它只是变得音频文件直接在SD卡上铺设。所以它不寻求低谷子folders.I'd喜欢用Mediastore,但我不得到它的ArrayList。也许你能帮助我,非常感谢和抱歉beeing一个小白)vinzenz

 进口的java.io.File;
进口java.io.FilenameFilter;
进口的java.util.ArrayList;
进口的java.util.HashMap;

进口de.me.musicplayer.SongsManager.FileExtensionFilter;

进口android.database.Cursor;
进口android.net.Uri;
进口android.provider.MediaStore;




公共类SongsManager {

最后弦乐MEDIA_PATH =新的String(/ SD卡/);
私人的ArrayList< HashMap的<字符串,字符串>> songsList =新的ArrayList< HashMap的<字符串,字符串>>();

//构造函数
公共SongsManager(){

}

/ **
 *功能读取SD卡所有的MP3文件
 *并存储在ArrayList中的细节
 * * /
公众的ArrayList< HashMap的<字符串,字符串>> getPlayList(){
    文件家庭=新的文件(MEDIA_PATH);

    如果(home.listFiles(新FileExtensionFilter())长度GT; 0){
        对于(文件文件:home.listFiles(新FileExtensionFilter())){
            HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
            song.put(songTitle,file.getName()子串(0,(file.getName()长度() -  4))。。);
            song.put(songPath,file.getPath());

            //添加每一首歌曲的SongList
            songsList.add(歌曲);
        }
    }
    //返回歌曲列表排列
    返回songsList;
}

/ **
 *分类过滤,它们是具有.MP3扩展名的文件
 * * /
类FileExtensionFilter实现了FilenameFilter {
    公共布尔接受(文件目录,字符串名称){
        返程(name.endsWith(MP3)|| name.endsWith()MP3。);
    }
}
}
 

解决方案

 最后弦乐MEDIA_PATH = Environment.getExternalStorageDirectory()
        .getPath()+/;
私人的ArrayList< HashMap的<字符串,字符串>> songsList =新的ArrayList< HashMap的<字符串,字符串>>();
私人字符串mp3Pattern =.MP3;

//构造函数
公共SongsManager(){

}

/ **
 *功能读取所有的MP3文件,并存储在细节
 * ArrayList的
 * * /
公众的ArrayList< HashMap的<字符串,字符串>> getPlayList(){
    的System.out.println(MEDIA_PATH);
    如果(MEDIA_PATH!= NULL){
        文件家庭=新的文件(MEDIA_PATH);
        文件[] listFiles = home.listFiles();
        如果(listFiles =空&安培;!&安培; listFiles.length大于0){
            对于(文件文件:listFiles){
                的System.out.println(file.getAbsolutePath());
                如果(file.isDirectory()){
                    scanDirectory(文件);
                } 其他 {
                    addSongToList(文件);
                }
            }
        }
    }
    //返回歌曲列表排列
    返回songsList;
}

私人无效scanDirectory(文件目录){
    如果(目录!= NULL){
        文件[] listFiles = directory.listFiles();
        如果(listFiles =空&安培;!&安培; listFiles.length大于0){
            对于(文件文件:listFiles){
                如果(file.isDirectory()){
                    scanDirectory(文件);
                } 其他 {
                    addSongToList(文件);
                }

            }
        }
    }
}

私人无效addSongToList(文件曲){
    如果(song.getName()。的endsWith(mp3Pattern)){
        HashMap的<字符串,字符串> songMap =新的HashMap<字符串,字符串>();
        songMap.put(songTitle
                。song.getName()子串(0,(song.getName()长度() -  4)));
        songMap.put(songPath,song.getPath());

        //添加每一首歌曲的SongList
        songsList.add(songMap);
    }
}
 

I want to write a class which gets all the mp3 files from the whole sd card. But actually it only gets the audio files laying directly on the sd card. so it doesnt search trough sub folders.I'd like to use Mediastore but i dont get it to an arraylist. Maybe you can help me, thanks a lot and sorry for beeing a noob ;) vinzenz

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import de.me.musicplayer.SongsManager.FileExtensionFilter;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;




public class SongsManager {

final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new         ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String,   String>();
            song.put("songTitle", file.getName().substring(0,   (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }
    }
    // return songs list array
    return songsList;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}
}
解决方案
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
        .getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";

// Constructor
public SongsManager() {

}

/**
 * Function to read all mp3 files and store the details in
 * ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList() {
    System.out.println(MEDIA_PATH);
    if (MEDIA_PATH != null) {
        File home = new File(MEDIA_PATH);
        File[] listFiles = home.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                System.out.println(file.getAbsolutePath());
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }
            }
        }
    }
    // return songs list array
    return songsList;
}

private void scanDirectory(File directory) {
    if (directory != null) {
        File[] listFiles = directory.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }

            }
        }
    }
}

private void addSongToList(File song) {
    if (song.getName().endsWith(mp3Pattern)) {
        HashMap<String, String> songMap = new HashMap<String, String>();
        songMap.put("songTitle",
                song.getName().substring(0, (song.getName().length() - 4)));
        songMap.put("songPath", song.getPath());

        // Adding each song to SongList
        songsList.add(songMap);
    }
}

这篇关于如何从SD卡上所有的android的所有音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:38