2012-01-15 136 views
0

我嘗試下面的代碼,它不設置鈴聲。 「ff」的logcat條目表示爲空,所以我猜這個URI沒有正確連接?,我似乎無法弄清楚我的代碼中哪裏出錯了。Android設置鈴聲失敗

String filepath =Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3"; 
       File ringtoneFile = new File(filepath); 
       ContentValues content = new ContentValues(); 
       content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath()); 
       content.put(MediaStore.MediaColumns.TITLE, "test"); 
       content.put(MediaStore.MediaColumns.SIZE, 215454); 
       content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg"); 
       content.put(MediaStore.Audio.Media.ARTIST, "artist"); 
       content.put(MediaStore.Audio.Media.DURATION, 230); 
       content.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
       content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); 
       content.put(MediaStore.Audio.Media.IS_ALARM, false); 
       content.put(MediaStore.Audio.Media.IS_MUSIC, false); 
       Log.i("BOOM", "the absolute path of the file is :"+ringtoneFile.getAbsolutePath()); 
       Uri uri = MediaStore.Audio.Media.getContentUriForPath(
       ringtoneFile.getAbsolutePath()); 
       Uri newUri = getApplicationContext().getContentResolver().insert(uri, content); 
       Uri ringtoneUri = newUri; 
       Log.i("ff","the ringtone uri is :"+ringtoneUri); 
       RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), 
       RingtoneManager.TYPE_RINGTONE,newUri); 

回答

0

其可能的數據對文件不準確並且導致問題。您可能需要改變:

audio/mpegaudio/mp3

duration到文件的實際持續時間。

你可能需要修正的另一件事是你的鈴聲文件聲明語句,嘗試改變它來搭配這樣的:

File k = new File(path, "mysong.mp3"); 

另外,我有一種感覺你的路太破..

Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3" 

爲什麼用這兩個「//」?

它應該是(例如):

../sdcard/media/ringtone 
+0

我已經在C#中已經工作(//)不知道哪裏出了問題,謝謝 – brux 2012-01-16 15:24:20