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

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

问题描述

向上看。 ive建立了一个简单的音乐应用程序,从sdcard读取wav文件并播放它们。
我如何访问默认的媒体目录?
这是我如何得到sdcard

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

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

文件sd = new File(Environment.getExternalStorageDirectory()); //这需要是用户可以访问的文件夹,像媒体

像往常一样,文档不会一个实际的使用示例,但它说 - 如果您使用API​​ 8级或更高版本,请使用getExternalFilesDir()打开一个表示应保存文件的外部存储目录的文件。该方法采用一个类型参数,指定所需的子目录的类型,例如DIRECTORY_MUSIC ...



我如何使用它?
谢谢



编辑:
如果我尝试用文件路径字符串填充微调数组,这会使它崩溃。

 文件路径= getExternalFilesDir(Environment.DIRECTORY_MUSIC); 
文件sd = new File(path,/ myFolder);

文件[] sdDirList = sd.listFiles(new WavFilter());
if(sdDirList!= null)
{
//排序微调
amountofiles = sdDirList.length;


array_spinner = new String [amountofiles];
......
final Spinner s =(Spinner)findViewById(R.id.spinner1); //这里崩溃

ArrayAdapter<?> adapter = new ArrayAdapter< Object>(this,
android.R.layout.select_dialog_item,array_spinner);

EDIT2:
好​​的,完成这个测试,应该写一个txt文件音乐目录。
i运行应用程序,没有txt文件在我可以找到的设备的任何地方写。

  //写入路径文件到
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();

String fname =mytest.txt;

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

//外部媒体可以写入
if(extState.equals(Environment.MEDIA_MOUNTED))
{
try {
//确保路径存在
boolean exists =(new File(path))。exists();
if(!exists){new File(path).mkdirs(); }

//打开输出流
FileOutputStream fOut = new FileOutputStream(path + fname);

fOut.write(Test.getBytes());

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

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



}

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

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

/////////////////// ///////////////////////////////////////////
最终编辑:
右,所以这将在设备音乐目录中找到一个名为test的文件夹。
如果它不存在,它将被创建。
(有些修复在这里完成,错误如果为空)然后列出目录中的文件并将其添加到数组。

  public void LoadSounds()throws IOException 
{
String extState = Environment.getExternalStorageState();
//将文件写入
的路径String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC +/ test)。getAbsolutePath();

if(!extState.equals(Environment.MEDIA_MOUNTED)){
// handle error here
}
else {
//做你的文件在这里工作

//确保路径存在
boolean exists =(new File(path))。exists();
//如果不创建它
if(!exists){new File(path).mkdirs(); }


文件sd = new File(path);
//这将返回一个包含外部存储文件夹中所有文件(目录和文件)
//的数组

File [] sdDirList = sd.listFiles();

if(sdDirList!= null)
{
//将文件添加到微调框数组
array_spinnerLoad = new String [sdDirList.length];
files = new String [sdDirList.length]; (int i = 0; i< sdDirList.length; i ++){

array_spinnerLoad [i] = sdDirList [i] .getName();


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

}
}
}


解决方案

如文档中所述, getExternalFilesDir()返回文件。文件对象可以表示文件或目录。



因此:

  File musicDirectory = new File(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天全站免登陆