Code example for File

Methods: canReadexistsisDirectorymkdirs

0
	public static File getOldPlaylistFile(String name) {
		File playlistDir = getPlaylistDirectory();
		return new File(playlistDir, name);
	} 
	public static File getPlaylistDirectory() {
		File playlistDir = new File(getSubsonicDirectory(), "playlists");
		ensureDirectoryExistsAndIsReadWritable(playlistDir);
		return playlistDir;
	} 
 
    public static File getAlbumArtFile(Context context, MusicDirectory.Entry entry) {
        File albumDir = getAlbumDirectory(context, entry);
        return getAlbumArtFile(albumDir);
    } 
 
    public static File getAlbumArtFile(File albumDir) {
        return new File(albumDir, Constants.ALBUM_ART_FILE);
    } 
 
    public static Bitmap getAlbumArtBitmap(Context context, MusicDirectory.Entry entry, int size) {
        File albumArtFile = getAlbumArtFile(context, entry);
        if (albumArtFile.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(albumArtFile.getPath());
			return (bitmap == null) ? null : Bitmap.createScaledBitmap(bitmap, size, size, true);
        } 
        return null; 
    } 
	 
	public static File getArtistDirectory(Context context, Artist artist) {
		File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getName()));
		return dir;
	} 
 
    public 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());
			if("unnamed".equals(album)) {
				album = fileSystemSafe(entry.getTitle());
			} 
            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()) {