2011-03-25 481 views
2

我正在開發一款Android應用程序,該應用程序允許用戶長按一個按鈕以將鈴聲保存爲鈴聲。我正在使用下面的代碼來這樣做。該代碼目前用於將文件保存在要使用的鈴聲列表中,但它不會自動將聲音設置爲默認鈴聲。我搜索了所有的東西,沒有多少運氣找到一個明確的指導,保存聲音作爲默認/活動鈴聲。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; 
    } 

回答

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