问题描述
我的目标是从正在从与在应用程序存储到SD卡中的文件集的用户通知声音。我用这code:
My goal is to set the users notification sound from a file that is stored onto the SD card from with in the application. I am using this code:
if(path != null){
File k = new File(path, "moment.mp3");
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "Some Name");
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = MainActivity.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri
);
//RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
Toast.makeText(this, "Notification Ringtone Set", Toast.LENGTH_SHORT).show();
}
在我这运行在设备上我不断收到错误:
When I run this on the device I keep getting the error:
06-12 15:19:36.741: ERROR/Database(2847): Error inserting is_alarm=false is_ringtone=false artist_id=35 is_music=false album_id=-1 title=My Song title duration=230 is_notification=true title_key=%D%\%%P%H%F%8%%R%<%R%B%4% mime_type=audio/mp3 date_added=1276370376 _display_name=moment.mp3 _size=215454 _data=/mnt/sdcard/Android/data/_MY APP PATH_/files/moment.mp3
06-12 15:19:36.741: ERROR/Database(2847): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
我见过别人使用这种技术,我无法找到任何有关哪些值确实需要在传递成功地将文件添加到了Android系统,以便它可以被设置为一个通知文件。
I have seen others using this technique and I can't find any documentation on which values actually need to be passed in to successfully add the file into the Android system so that it can be set as a notification.
推荐答案
纠正你的code到这一点:
Correct your code to this:
if(path != null){
File k = new File(path, "moment.mp3");
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri
);
记得要小心测试这个code!它只是如实作品第一次。如果您尝试再次运行相同的code,你会试图推在MediaStore的表中的重复条目,并SQLite数据库不会让你,让你完全一样的错误。你要么重命名文件,并添加它的另一个实例,或者进去,删除该条目,然后重试。要做到这一点,你可以使用RingDroid。只要确保你拥有所有的音频可视,并搜索文件名,然后从那里将其删除。或者,你可以看看他们的源代码中删除从表中。
Remember to be careful about testing this code! It only truthfully works the first time. If you try running the same code again, you'll be trying to shove a duplicate entry in MediaStore's table, and the SQLite database won't allow you, giving you that exact same error. You have to either rename your file and add another instance of it, or go in, remove the entry, and then try again. To do this, you could use RingDroid. Just make sure you have all audio visible, and search for your filename, then delete it from there. Alternately, you could look at their source for deleting from the table.
这篇关于从SD卡的文件设置铃声的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!