Android铃声以编程方式设置?

时间:2015-03-25 13:12:46

标签: android 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

2 个答案:

答案 0 :(得分:6)

重新排列代码行.. 根据我的理解,在删除旧铃声之前插入新的铃声。只需用上面的代码替换。

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

答案 1 :(得分:0)

也许你需要先删除它,我已经陷入困境一天了..

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