2

我正在开发一个 Android 应用程序,该应用程序允许用户长按按钮以将声音保存为铃声。我正在使用下面的代码来做到这一点。该代码当前用于将文件保存在要使用的铃声列表中,但它不会自动将声音设置为默认铃声。我已经四处搜索,但没有找到关于将声音保存为默认/活动铃声的明确指南。

截至目前,用户可以长按该按钮,然后进入“菜单”>“声音”>“电话铃声”菜单并从列表中进行选择,但是当我知道可以将其简单地设置为立即激活铃声。

关于我缺少什么的任何见解?非常感激!

public boolean saveas(int ressound){  
      byte[] buffer=null;  
      InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
      int size=0;  

      try {  
       size = fIn.available();  
       buffer = new byte[size];  
       fIn.read(buffer);
       fIn.close();  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }  

      String path="/sdcard/media/audio/ringtones/";  
      String filename="ADTone"+".ogg";  

      boolean exists = (new File(path)).exists();  
      if (!exists){new File(path).mkdirs();}  

      FileOutputStream save;  
      try {  
       save = new FileOutputStream(path+filename);  
       save.write(buffer);  
       save.flush();  
       save.close();  
      } catch (FileNotFoundException e) {  
       // TODO Auto-generated catch block  
       return false;  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }      

      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

      File k = new File(path, filename);  

      ContentValues values = new ContentValues();  
      values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
      values.put(MediaStore.MediaColumns.TITLE, "AD Ringtone");  
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
      values.put(MediaStore.Audio.Media.ARTIST, "adtone ");  
      values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
      values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
      values.put(MediaStore.Audio.Media.IS_ALARM, true);  
      values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

      //Insert it into the database  
      this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  


      return true;  
     }   
4

1 回答 1

2

不知道你是否知道这个,但我最近才知道。将您的插入数据库行替换为:

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(
                    YOURACTIVITYNAME.this,
              RingtoneManager.TYPE_RINGTONE,
              newUri
            );
于 2011-06-26T20:34:45.677 回答