访问Android的媒体目录? [英] access android media directory?

查看:119
本文介绍了访问Android的媒体目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EY起来。香港专业教育学院建立了一个简单的音乐应用程序,从SD卡读取wav文件并播放它们。 我如何访问默认的媒体目录? 这是我得到的SD卡

 公共无效LoadSounds()抛出IOException异常
    {
        串extState = Environment.getExternalStorageState();

         如果(!extState.equals(Environment.MEDIA_MOUNTED)){
             //这里处理错误
         }
         其他 {

         文件SD =新的文件(Environment.getExternalStorageDirectory()); //这需要一个文件夹中的用户可访问,如媒体
 

像往常一样的文档不给使用的实际例子,但它说这一点 - 如果你使用的API级别8或更高,使用getExternalFilesDir()打开重新$ P $文件psents外部存储目录,你应该保存文件。这个方法有一个类型参数,该参数指定要的子目录中的类型,如DIRECTORY_MUSIC ...

我如何使用它? 谢谢

编辑: 这使得它崩溃,如果我试图填补了微调阵列,文件路径字符串。

 文件路径= getExternalFilesDir(Environment.DIRECTORY_MUSIC);
            文件SD =新的文件(路径,/ MyFolder的);

sdDirList = sd.listFiles(新WavFilter())文件[];
         如果(sdDirList!= NULL)
         {
             //排序的微调
            amountofiles = sdDirList.length;


             array_spinner =新的String [amountofiles]
......
最后的微调S =(微调)findViewById(R.id.spinner1); //此处崩溃

        ArrayAdapter<>适配器=新的ArrayAdapter<对象>(这一点,
        android.R.layout.select_dialog_item,array_spinner);
 

EDIT2: 好了,所以香港专业教育学院做这个测试应该写一个txt文件,以音乐目录。 我运行的应用程序,没有任何txt文件被任意位置写入设备,我可以找到。

  //路径写入文件
        字符串路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();

        字符串FNAME =mytest.txt;

        //外部媒体的当前状态
        串extState = Environment.getExternalStorageState();

        //外置媒体可以将其写入
        如果(extState.equals(Environment.MEDIA_MOUNTED))
        {
            尝试 {
                //确保存在的路径
                布尔存在=(新文件(路径))存在();
                如果(存在!){新的文件(路径).mkdirs(); }

                //打开输出流
                FileOutputStream中FOUT =新的FileOutputStream(路径+ FNAME);

                fOut.write(测试.getBytes());

                //关闭输出流
                fOut.flush();
                fOut.close();

            }赶上(IOException异常IOE){
                ioe.printStackTrace();
            }



    }
 

另一个编辑:我会得到这个工作! 所以,如果我用这条线它创建的名为Musictest的SD卡的文件夹。不明白??

 字符串路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC +测试)getAbsolutePath()。
 

/////////////////////////////////////////////// ///////////////////// 最后编辑: 右所以这将寻找一个名为test的设备音乐目录文件夹。 如果它不存在,它会被创建。 (部分固定在这里完成,错误,如果是空的)它,然后列出该目录中的文件,并将它们添加到一个数组。

 公共无效LoadSounds()抛出IOException异常
    {
        串extState = Environment.getExternalStorageState();
        //路径写入文件
            字符串路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC +/测试)getAbsolutePath()。

         如果(!extState.equals(Environment.MEDIA_MOUNTED)){
             //这里处理错误
         }
         其他 {
             //这里做你的档案工作

                //确保存在的路径
                布尔存在=(新文件(路径))存在();
                //如果没有创建它
                如果(存在!){新的文件(路径).mkdirs(); }


         文件SD =新的文件(路径);
         //这将返回所有文件(目录和文件)数组
         //在外部存储文件夹

         文件[] sdDirList = sd.listFiles();

         如果(sdDirList!= NULL)
         {
             //将文件添加到微调阵列
             array_spinnerLoad =新的String [sdDirList.length]
             文件=新的String [sdDirList.length]

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

         array_spinnerLoad [I] = sdDirList [I] .getName();
        文件[I] = sdDirList [I] .getAbsolutePath();
         }

         }
         }
    }
 

解决方案

中提到的文档,<一个href="http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29">getExternalFilesDir()返回文件。而File对象可以重新present任何文件或目录。

因此​​:

 文件musicDirectory =新的文件(getExternalFilesDir(Environment.DIRECTORY_MUSIC));
 

会给你打的对象。

ey up. ive built a simple music app that reads wav files from the sdcard and plays them. how do i access the default media directory? this is how i get the sdcard

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

as usual the docs dont give an actual example of usage but it says this - If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC...

how do i use it? thank you

edit: this makes it crash if i try to fill a spinner array with file path Strings.

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

EDIT2: ok so ive done this test that is supposed to write a txt file to the music directory. i run the app, no txt file is written anywhere on the device i can find.

 // Path to write files to
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();

        String fname = "mytest.txt";

        // Current state of the external media
        String extState = Environment.getExternalStorageState();

        // External media can be written onto
        if (extState.equals(Environment.MEDIA_MOUNTED))
        {
            try {
                // Make sure the path exists
                boolean exists = (new File(path)).exists();  
                if (!exists){ new File(path).mkdirs(); }  

                // Open output stream
                FileOutputStream fOut = new FileOutputStream(path + fname);

                fOut.write("Test".getBytes());

                // Close output stream
                fOut.flush();
                fOut.close();

            } catch (IOException ioe) {
                ioe.printStackTrace();
            }



    }

another edit: i will get this working!! so if i use this line it creates a folder on the sdcard called 'Musictest'. dont understand??

 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "test").getAbsolutePath();

//////////////////////////////////////////////////////////////////// Final Edit: right so this will look for a folder called test in the devices music directory. if it doesnt exist, it will be created. (some fixing to be done here, error if empty) it then lists the files in the directory and adds them to an array.

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

         }
         }
    }

解决方案

as mentioned in the docs, getExternalFilesDir() return File. And File object can represent either file or directory.

Therefore:

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

Will give you the object to play with.

这篇关于访问Android的媒体目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆