3

I'm working on a media file organization program for Android and I want to move media files into a directory structure such as root/Artist/Album/Song.flac. I am looping through a data structure containing this information and making folders using mkdirs(). When I browse the created folders (using OI File Manager or File Expert) I can enter the first level directory and see the names of second-level directories, but they are indicated as 0 byte files. At first I thought they really were just files and tried again, but if I browse them in ADB they are files. The permissions are the problem:

For instance, I have my root at /mnt/storage/FLAC:

ls -l
drwxrw-rw-   4   app_63   app_63   16 Dec 3 20:42 Artist_1
drwxrw-rw-   3   app_63   app_63   16 Dec 3 20:42 Artist_2
...

The second level directories (/mnt/storage/FLAC/Artist_1 etc) have the same permissions on the Album folders.

If I manually edit the permissions with chmod, setting both level folders to drwxrwxrwx (777), I can navigate into them and play the files in file manager. This can be done with chmod +x.

Here is the code I'm using:

public void organizeFiles(String rootDir, ArrayList<libraryElementArtist> libraryData)
{
    for(int i = 0; i < libraryData.size(); i++)
    {
        //create artist folder
        //{
        //    (new File(rootDir + File.separator + libraryData.get(i).name)).mkdirs();
        //}
        for(int j = 0; j < libraryData.get(i).albums.size(); j++)
        {
            //create album folder
            (new File(rootDir + File.separator + libraryData.get(i).name + File.separator + libraryData.get(i).albums.get(j).name + File.separator)).mkdirs();
            for(int k = 0; k < libraryData.get(i).albums.get(j).songs.size(); k++)
            {
                //move song into folder
                FileOperations.moveFile(libraryData.get(i).albums.get(j).songs.get(k).filename, rootDir + "/" + libraryData.get(i).name + "/" + libraryData.get(i).albums.get(j).name);
            }
        }
    }
}

This is the moveFile function:

public static boolean moveFile(String sourcefile, String dest)
{
    File file = new File(sourcefile);
    File dir = new File(dest);
    return file.renameTo(new File(dir, file.getName()));
}

I have this line in my Manifest file:

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

The storage is 16GB internal ext3 on my device. How should I add this permissions fix to my code?

1 Answers1

0

You need the following permission in your App Manifest:

android.permission.WRITE_EXTERNAL_STORAGE

Ref:

Storage

havexz
  • 9,550
  • 2
  • 33
  • 29
  • Sorry, I already had this in my Manifest file but the formatting made it hidden in my question. I've edited my question. The problem isn't that it cannot make the directory, it's that it makes it with the wrong permissions (the directory is created just fine and to the root shell everything went perfectly, just other apps don't have permission to use it). – CalcProgrammer1 Dec 04 '11 at 05:35