Code example for File

Methods: canRead, canWrite, exists, isDirectory, mkdirs

0
        } 
        return null; 
    } 
 
    public static File getAlbumArtDirectory() {
        File albumArtDir = new File(getSubsonicDirectory(), "artwork");
        ensureDirectoryExistsAndIsReadWritable(albumArtDir);
        ensureDirectoryExistsAndIsReadWritable(new File(albumArtDir, ".nomedia"));
        return albumArtDir;
    } 
 
    private static File getAlbumDirectory(Context context, MusicDirectory.Entry entry) {
        File dir;
        if (entry.getPath() != null) {
            File f = new File(fileSystemSafeDir(entry.getPath()));
            dir = new File(getMusicDirectory(context).getPath() + "/" + (entry.isDirectory() ? f.getPath() : f.getParent()));
        } else { 
            String artist = fileSystemSafe(entry.getArtist());
            String album = fileSystemSafe(entry.getAlbum());
            dir = new File(getMusicDirectory(context).getPath() + "/" + artist + "/" + album);
        } 
        return dir;
    } 
 
    public static void createDirectoryForParent(File file) {
        File dir = file.getParentFile();
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.e(TAG, "Failed to create directory " + dir);
            } 
        } 
    } 
 
    private static File createDirectory(String name) {
        File dir = new File(getSubsonicDirectory(), name);
        if (!dir.exists() && !dir.mkdirs()) {
            Log.e(TAG, "Failed to create " + name);
        } 
        return dir;
    } 
 
    public static File getSubsonicDirectory() {
        return new File(Environment.getExternalStorageDirectory(), "subsonic");
    } 
 
    public static File getDefaultMusicDirectory() {
        return DEFAULT_MUSIC_DIR;
    } 
 
    public static File getMusicDirectory(Context context) {
        String path = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, DEFAULT_MUSIC_DIR.getPath());
        File dir = new File(path);
        return ensureDirectoryExistsAndIsReadWritable(dir) ? dir : getDefaultMusicDirectory();
    } 
 
    public static boolean ensureDirectoryExistsAndIsReadWritable(File dir) {
        if (dir == null) {
            return false; 
        } 
 
        if (dir.exists()) {
            if (!dir.isDirectory()) {
                Log.w(TAG, dir + " exists but is not a directory.");
                return false; 
            } 
        } else { 
            if (dir.mkdirs()) {
                Log.i(TAG, "Created directory " + dir);
            } else { 
                Log.w(TAG, "Failed to create directory " + dir);
                return false; 
            } 
        } 
 
        if (!dir.canRead()) {
            Log.w(TAG, "No read permission for directory " + dir);
            return false; 
        } 
 
        if (!dir.canWrite()) {
            Log.w(TAG, "No write permission for directory " + dir);
            return false; 
        } 
        return true; 
    }