从 SD 卡文件设置铃声通知
我的目标是从应用程序中存储到 SD 卡上的文件设置用户通知声音。我正在使用此代码:
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();
}
当我在设备上运行此代码时,我不断收到错误:
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系统这样可以将其设置为通知。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的代码更正为:
记住测试此代码时要小心!只有第一次才真正有效。如果您尝试再次运行相同的代码,您将尝试在 MediaStore 的表中插入重复的条目,而 SQLite 数据库将不允许您这样做,从而给出完全相同的错误。您必须重命名文件并添加它的另一个实例,或者进入,删除该条目,然后重试。为此,您可以使用 RingDroid。只需确保所有音频可见,然后搜索您的文件名,然后从那里将其删除。或者,您可以查看它们的来源以从表中删除。
Correct your code to this:
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.
韦斯顿,删除条目数据库后,您是对的,添加它将解决此问题
删除条目的代码
Weston you are right after deleting entry form database and the adding it will work out for this problem
Code for deleting entry
请记住将“uses-permission”标签放入
AndroidManifest.xml
中,并将android:name
属性设置为android.permission.WRITE_SETTINGS。
Remember to put the "uses-permission" tag in the
AndroidManifest.xml
with theandroid:name
attribute set toandroid.permission.WRITE_SETTINGS.