如何在j2me中获取所有.mp3文件(来自媒体卡和设备内存)?

时间:2014-03-05 01:13:14

标签: file java-me mp3

无论如何要在j2me中获取所有.mp3文件列表,如android。 如果不是什么是获取所有mp3文件的最佳方式。

感谢。

1 个答案:

答案 0 :(得分:0)

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.file.*;

/**
 * @author crazywizard
 */
public class RootMIDlet extends MIDlet implements CommandListener{
    private List list;
    private Command exitCommand;
    private Hashtable allowed_types;

    public RootMIDlet(){
        /* Construct MIDlet */
        //Define the file types of interest here
        allowed_types = new Hashtable();
        allowed_types.put("mp3", "audio/mp3");        
    }

    public void startApp() {
        list = new List("My Files", List.IMPLICIT);
        exitCommand = new Command("Exit", Command.EXIT, 0);
        list.addCommand(exitCommand);
        list.setCommandListener(this);
        Display.getDisplay(this).setCurrent(list);
        getRootList();        
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void getRootList(){
        //Get ALL available filesystem mounts
        Enumeration drives = FileSystemRegistry.listRoots();
        while(drives.hasMoreElements()){
            //Iterate through the drives
            String driveString = drives.nextElement().toString();
            list.append("Searching "+driveString, null);
            getFileList("file:///"+driveString);
        }
    }//--End of getRootList()

    public void getFileList(String path){
        try {
            //Open path
            FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
            //Get list of ALL files and dirs
            Enumeration filelist = fc.list();   //Get Hidden files as well --fc.list("*", true);
            while(filelist.hasMoreElements()){
                String filename = (String)filelist.nextElement();                
                FileConnection fc1 = (FileConnection)Connector.open(path+filename, Connector.READ);
                //Check for dirs
                if(fc1.isDirectory()){                    
                    //Apply recursion here
                    getFileList("file://"+fc1.getPath()+filename);  //Note use of *double // instead of triple ///. Adjust accordingly according to phone platform
                }else{
                    //Filter for only allowed types == .mp3
                    String file_type = getFileType(filename).toLowerCase();
                    if(!allowed_types.containsKey(file_type)){
                        //Do Nothing and Pass
                        continue;
                    }
                    list.append(fc1.getPath()+filename, null);
                }
            }
            //Clean resources
            fc.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            Display.getDisplay(this).setCurrent(new Alert("Error", ioe.toString(), null, AlertType.ERROR));
        } catch (Exception ex) {
            ex.printStackTrace();
            Display.getDisplay(this).setCurrent(new Alert("Error", ex.toString(), null, AlertType.ERROR));
        }
    }

    /* Helper Method */
    private String getFileType(String fileName){
        //Get file extension from fileName
        char ch = '.';
        int index = fileName.lastIndexOf((int)ch);
        return fileName.substring(index+1);
    }//--End of getFileType(String)

    public void commandAction(Command c, Displayable d) {
        if(c == exitCommand){
            notifyDestroyed();
        }
    }
}