0

我正在创建一个播放用户音乐的 android 应用程序。我已经让它在模拟器上正常工作,但是当我将它安装在手机上时它不起作用,它在这一行崩溃:

int songIndex = new Random().nextInt(songsList.size());

因为 songList.size() 返回 0,因为在手机上运行时似乎找不到音乐。我在手机里放了一张 Micro SD 卡,并在上面加载了音乐(在根文件夹中)。我正在使用以下内容来获取路径:

final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();

在模拟器和手机上,从这里返回的字符串是 /mnt/sdcard。但它只适用于模拟器。我还在清单文件中包含以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

有任何想法吗?

编辑:

我没有包括这个,因为我认为它不会有太大帮助,但这是我用来实际获取歌曲列表的代码:

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());

            //Add song to song list
            songsList.add(song);
        }
    }

    return songsList;
}
4

2 回答 2

1

怎么样

return Environment.getExternalStorageDirectory().toString() + "/Music";

这将返回到内部 SD 挂载点的路径,例如“/mnt/sdcard”

这是一种比路径中的硬编码更好的编码方式。

编辑

要使其在所有设备上运行,请尝试使用此线程中的以下代码,他们在其中讨论 Android 除了外部存储之外没有“外部 SD”的概念。然后,根据他得到的所有答案和评论,OP 针对他的问题提出了以下解决方案。

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import android.os.Environment;
import android.util.Log;

public class ExternalStorage {

public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";

/**
 * @return True if the external storage is available. False otherwise.
 */
public static boolean isAvailable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

public static String getSdCardPath() {
    return Environment.getExternalStorageDirectory().getPath() + "/";
}

/**
 * @return True if the external storage is writable. False otherwise.
 */
public static boolean isWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;

}

/**
 * @return A map of all storage locations available
 */
public static Map<String, File> getAllStorageLocations() {
    Map<String, File> map = new HashMap<String, File>(10);

    List<String> mMounts = new ArrayList<String>(10);
    List<String> mVold = new ArrayList<String>(10);
    mMounts.add("/mnt/sdcard");
    mVold.add("/mnt/sdcard");

    try {
        File mountFile = new File("/proc/mounts");
        if(mountFile.exists()){
            Scanner scanner = new Scanner(mountFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.startsWith("/dev/block/vold/")) {
                    String[] lineElements = line.split(" ");
                    String element = lineElements[1];

                    // don't add the default mount path
                    // it's already in the list.
                    if (!element.equals("/mnt/sdcard"))
                        mMounts.add(element);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        File voldFile = new File("/system/etc/vold.fstab");
        if(voldFile.exists()){
            Scanner scanner = new Scanner(voldFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.startsWith("dev_mount")) {
                    String[] lineElements = line.split(" ");
                    String element = lineElements[2];

                    if (element.contains(":"))
                        element = element.substring(0, element.indexOf(":"));
                    if (!element.equals("/mnt/sdcard"))
                        mVold.add(element);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


    for (int i = 0; i < mMounts.size(); i++) {
        String mount = mMounts.get(i);
        if (!mVold.contains(mount))
            mMounts.remove(i--);
    }
    mVold.clear();

    List<String> mountHash = new ArrayList<String>(10);

    for(String mount : mMounts){
        File root = new File(mount);
        if (root.exists() && root.isDirectory() && root.canWrite()) {
            File[] list = root.listFiles();
            String hash = "[";
            if(list!=null){
                for(File f : list){
                    hash += f.getName().hashCode()+":"+f.length()+", ";
                }
            }
            hash += "]";
            if(!mountHash.contains(hash)){
                String key = SD_CARD + "_" + map.size();
                if (map.size() == 0) {
                    key = SD_CARD;
                } else if (map.size() == 1) {
                    key = EXTERNAL_SD_CARD;
                }
                mountHash.add(hash);
                map.put(key, root);
            }
        }
    }

    mMounts.clear();

    if(map.isEmpty()){
             map.put(SD_CARD, Environment.getExternalStorageDirectory());
    }
    return map;
}
}

用法

Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();
File sdCard = externalLocations.get(ExternalStorage.SD_CARD);
File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
于 2013-10-06T11:52:01.837 回答
0

我认为原因是,您已经在调试模式下运行了将手机连接到 PC 的应用程序。而且您的手机处于大容量存储模式,因此当连接到 PC 时,您的 SD 卡将被卸载。这就是为什么您没有在列表中找到歌曲的原因。希望能帮助到你。

于 2013-10-06T11:58:32.100 回答