user1033558
user1033558

Reputation: 161

access android media directory?

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

         }
         }
    }

Upvotes: 4

Views: 20246

Answers (1)

ariefbayu
ariefbayu

Reputation: 21979

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.

Upvotes: 14

Related Questions