Android 以编程方式设置铃声?

Android ringtone set programmatically?

我想在录制后为我的应用程序设置铃声 voice.Ringtone 只会正确设置一次它会被设置为铃声,而再次设置它不能正常工作我已经添加了我的代码:

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

我的错误是::

新铃声:

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

现有铃声:

TAG    the ringtone uri is :null

重新排列代码行.. 根据我的理解,你在删除旧的之前插入了新的铃声 one.Just 用这个替换上面的代码。

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

也许你需要先删除它,我被困了一天..

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