user4272859
user4272859

Reputation:

Android ringtone set programmatically?

I want to implement set ringtone to my application after recording voice.Ringtone will set correctly only one time it will be set as ringtone while set again its not working properly here i have added my code:

String filepath ="/sdcard/sample/"+currentName+"";
                            System.out.println("/sdcard/sample/"+currentName+"");


                            File ringtoneFile = new File(filepath);

                            ContentValues content = new ContentValues();
                            content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
                            content.put(MediaStore.MediaColumns.TITLE, currentName);
                            content.put(MediaStore.MediaColumns.SIZE, 215454);
                            content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                            //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
                            content.put(MediaStore.Audio.Media.DURATION, 230);
                            content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                            content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                            content.put(MediaStore.Audio.Media.IS_ALARM, true);
                            content.put(MediaStore.Audio.Media.IS_MUSIC, true);

                        String Ringtonepath= "content://media/internal/audio/media/297";
                            Uri Ringtone1 = Uri.parse(Ringtonepath);   
                            //Insert it into the database
                            Log.i("TAG", "the absolute path of the file is :"+
                                    ringtoneFile.getAbsolutePath());
                            Uri uri = MediaStore.Audio.Media.getContentUriForPath(
                                    ringtoneFile.getAbsolutePath());
                            Uri newUri = getContentResolver().insert(uri, content);
                            System.out.println("uri=="+uri);

                            Log.i("TAG","the ringtone uri is :"+newUri);
                            //   getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                            //           null);

                               RingtoneManager.setActualDefaultRingtoneUri(
                                       getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                                       newUri);

My Error is::

New ringtone:

TAG    the ringtone uri is :content://media/internal/audio/media/297

Existing Ringtone:

TAG    the ringtone uri is :null

Upvotes: 1

Views: 7391

Answers (2)

cmnewfan
cmnewfan

Reputation: 129

maybe you need to delete it first, i have been caught in the trouble for a day..

getContentResolver().delete(uri, MediaStore.MediaColumns.DATA
                            + "=\"" + file.getAbsolutePath() + "\"", null);

Upvotes: 0

user3441799
user3441799

Reputation:

Rearrange the code lines .. From my understanding you insert new ringtone before deleting the old one.Just replace the above code with this.

 String filepath ="/sdcard/sample/"+currentName+"";
                            System.out.println("/sdcard/sample/"+currentName+"");


                            File ringtoneFile = new File(filepath);

                            ContentValues content = new ContentValues();
                            content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
                            content.put(MediaStore.MediaColumns.TITLE, currentName);
                            content.put(MediaStore.MediaColumns.SIZE, 215454);
                            content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                            //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
                            content.put(MediaStore.Audio.Media.DURATION, 230);
                            content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                            content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                            content.put(MediaStore.Audio.Media.IS_ALARM, true);
                            content.put(MediaStore.Audio.Media.IS_MUSIC, true);

                        String Ringtonepath= "content://media/internal/audio/media/297";
                            Uri Ringtone1 = Uri.parse(filepath);   
                            //Insert it into the database
                            Log.i("TAG", "the absolute path of the file is :"+
                                    ringtoneFile.getAbsolutePath());
                            Uri uri = MediaStore.Audio.Media.getContentUriForPath(
                                    ringtoneFile.getAbsolutePath());




                            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
                                      null);
                            Uri newUri = getContentResolver().insert(uri, content);
                            System.out.println("uri=="+uri);
                            Log.i("TAG","the ringtone uri is :"+newUri);
                               RingtoneManager.setActualDefaultRingtoneUri(
                                       getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                                       newUri);

Upvotes: 6

Related Questions