简体   繁体   中英

Android set ringtone failure

I try the following code and it doesn't set the ringtone. The logcat entry for "ff" says null so I guess the URI isnt being concatenated properly?, I cant seem to figure out where in my code I am going wrong.

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

Its possible data is not accurate for the file and its causing a problem. You may need to change:

audio/mpeg to audio/mp3

and duration to the actual duration of the file.

Another thing you may want to fix is your ringtone file declaration statement, try changing it to match this:

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

Also, I have a feeling your path is broken too..

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

why with the two "//"?

It should be (for example):

../sdcard/media/ringtone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM