As a personal project, I've been trying to make an app that lists the music in my device and you can set any of this the default ringtone.
First, I get all the music in my device.
public void getSongList() {
ContentResolver musicResolver = getContentResolver();
musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
Log.d("helena", "urimusic : "+musicUri);
finalpath = musicUri.getEncodedPath();
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
//Log.d("helena",""+data);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
Song cancion = new Song(thisArtist,thisId,thisTitle);
songList.add(cancion);
}
while (musicCursor.moveToNext());
}
}
then, I show the titles in a ListView: when the user click on an element I call:
public void setRingTone(int index){
String path = this.finalpath; //got it from uri
String songName = songList.get(index).getTitle();
String artist = songList.get(index).getArtist();
File k = new File(path, songName );
Log.d("helena","song:"+songName +", path:"+path+", absolutepath:"+k.getAbsolutePath());
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,songName );
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, artist);
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(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
Toast.makeText(this,"ringtone changed",Toast.LENGTH_SHORT).show();
}
It shows the song name as the default ringtone, but doesn't make any sound in the systems>sounds>ringtone, and when I get a call, it plays the previous (default) ringtone.
Any advice?