private static String getCoverFromFolder(Context context, Media media) { File f = Util.URItoFile(media.getLocation()); if (f != null && f.getParentFile() != null && f.getParentFile().listFiles() != null) for (File s : f.getParentFile().listFiles()) { if (s.getAbsolutePath().endsWith("png") || s.getAbsolutePath().endsWith("jpg")) return s.getAbsolutePath(); } return null; }
public static void setRingtone(Media song, Activity activity) { File newringtone = Util.URItoFile(song.getLocation()); if (!newringtone.exists()) { Toast.makeText( activity.getApplicationContext(), activity.getString(R.string.ringtone_error), Toast.LENGTH_SHORT) .show(); return; } ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, newringtone.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, song.getTitle()); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*"); values.put(MediaStore.Audio.Media.ARTIST, song.getArtist()); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); values.put(MediaStore.Audio.Media.IS_ALARM, false); values.put(MediaStore.Audio.Media.IS_MUSIC, false); Uri uri = MediaStore.Audio.Media.getContentUriForPath(newringtone.getAbsolutePath()); Uri newUri; try { activity .getContentResolver() .delete( uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null); newUri = activity.getContentResolver().insert(uri, values); RingtoneManager.setActualDefaultRingtoneUri( activity.getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); } catch (Exception e) { Toast.makeText( activity.getApplicationContext(), activity.getString(R.string.ringtone_error), Toast.LENGTH_SHORT) .show(); return; } Toast.makeText( activity.getApplicationContext(), activity.getString(R.string.ringtone_set, song.getTitle()), Toast.LENGTH_SHORT) .show(); }