Android ringtone set programmatically?

前端 未结 2 1605
不思量自难忘°
不思量自难忘° 2020-12-21 22:39

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

2条回答
  •  执念已碎
    2020-12-21 23:32

    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);
    

提交回复
热议问题