访问android媒体目录?

访问android媒体目录?,android,directory,media,Android,Directory,Media,别激动。我构建了一个简单的音乐应用程序,可以从SD卡读取wav文件并播放它们。 如何访问默认媒体目录? 这就是我获得SD卡的方式 public void LoadSounds() throws IOException { String extState = Environment.getExternalStorageState(); if(!extState.equals(Environment.MEDIA_MOUNTED)) {

别激动。我构建了一个简单的音乐应用程序,可以从SD卡读取wav文件并播放它们。 如何访问默认媒体目录? 这就是我获得SD卡的方式

public void LoadSounds() throws IOException
    {
        String extState = Environment.getExternalStorageState();

         if(!extState.equals(Environment.MEDIA_MOUNTED)) {
             //handle error here
         }
         else {

         File sd = new File(Environment.getExternalStorageDirectory ()); //this needs to be a folder the user can access, like media
像往常一样,文档没有给出实际的使用示例,但它说明了这一点——如果您使用的是API级别8或更高的API,请使用getExternalFilesDir()打开一个文件,该文件表示应该保存文件的外部存储目录。此方法接受一个类型参数,该参数指定所需子目录的类型,例如DIRECTORY\u MUSIC

我如何使用它? 多谢各位

编辑: 如果我试图用文件路径字符串填充微调器数组,这会导致崩溃

File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
            File sd = new File(path, "/myFolder");

File[] sdDirList = sd.listFiles(new WavFilter()); 
         if (sdDirList != null)
         {   
             //sort the spinner 
            amountofiles = sdDirList.length;


             array_spinner=new String[amountofiles];
......
final Spinner s = (Spinner) findViewById(R.id.spinner1); //crashes here

        ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,
        android.R.layout.select_dialog_item, array_spinner);
另一个编辑:我会让这个工作!! 因此,如果我使用这一行,它会在SD卡上创建一个名为“Musictest”的文件夹。不明白

 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "test").getAbsolutePath();
//////////////////////////////////////////////////////////////////// 最终编辑: 这将在设备音乐目录中查找名为test的文件夹。 如果它不存在,它将被创建。 (此处需要进行一些修复,如果为空则出错)然后列出目录中的文件并将其添加到数组中

 public void LoadSounds() throws IOException
    {
        String extState = Environment.getExternalStorageState();
        // Path to write files to
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/test").getAbsolutePath();

         if(!extState.equals(Environment.MEDIA_MOUNTED)) {
             //handle error here
         }
         else {
             //do your file work here 

                // Make sure the path exists
                boolean exists = (new File(path)).exists(); 
                //if not create it
                if (!exists){ new File(path).mkdirs(); }


         File sd = new File(path);
         //This will return an array with all the Files (directories and files)
         //in the external storage folder

         File[] sdDirList = sd.listFiles(); 

         if (sdDirList != null)
         {   
             //add the files to the spinner array
             array_spinnerLoad=new String[sdDirList.length];
             files = new String[sdDirList.length];

         for(int i=0;i<sdDirList.length;i++){

         array_spinnerLoad[i] = sdDirList[i].getName();
        files[i] = sdDirList[i].getAbsolutePath();
         }

         }
         }
    }
public void LoadSounds()引发IOException
{
字符串extState=Environment.getExternalStorageState();
//将文件写入的路径
String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC+“/test”).getAbsolutePath();
如果(!extState.equals(Environment.MEDIA_MOUNTED)){
//在这里处理错误
}
否则{
//你的档案在这里工作吗
//确保路径存在
布尔存在=(新文件(路径)).exists();
//如果没有,创建它
如果(!exists){new File(path).mkdirs();}
文件sd=新文件(路径);
//这将返回一个包含所有文件(目录和文件)的数组
//在外部存储文件夹中
文件[]sdDirList=sd.listFiles();
if(sdDirList!=null)
{   
//将文件添加到微调器阵列
数组_spinnerLoad=新字符串[sdDirList.length];
files=新字符串[sdDirList.length];
对于文档中提到的(int i=0;i),return.And File对象可以表示文件或目录

因此:

File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_MUSIC));

将为您提供可玩的对象。

检查这里很好。如果有帮助,请不要忘记投票并将其标记为接受答案:啊,发言太早,它只是崩溃了,我还必须在其中放入一个字符串。File sd=new File(getExternalFilesDir(Environment.DIRECTORY\u MUSIC),mysterysting);我用文件名填充微调器适配器,它在这里崩溃。如果我对文件使用字符串(我知道存在sdcard/myfolder)它是有效的。嗯……这超出了当前问题的范围。我想你应该再问一个关于这个问题的问题。不,它在问题的范围内。我问如何访问媒体目录。如果我使用你的原始答案代码,即使我知道它们在那里,它也不会返回任何文件。如果我尝试获取已知目录(例如sdcard/myfolder)的字符串路径。在这种情况下,我使用它崩溃(返回null)的事实作为指示,它没有读取媒体目录。如果我使用日志告诉我这一点,将是相同的最终结果。似乎目录\u Music不保证在真实设备上访问名为Music的文件夹。
File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_MUSIC));