SQLiteConstraintException:错误代码19:设置铃声时约束失败

时间:2013-07-14 12:44:10

标签: android database constraints

这是我的代码。

ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        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);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile
                .getAbsolutePath());
        Uri newUri = mCr.insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(RingToneActivity.this,
                    RingtoneManager.TYPE_RINGTONE, newUri);

        } catch (Throwable t) {
            Log.d("Eroor", "catch exception");
        }

但我收到错误......

07-14 18:07:09.569: E/Database(7505): Error inserting album_id=-1 title=afsdfa is_notification=true title_key=Q-Q7QQQ3Q7Q-Q mime_type=audio/* date_added=1373805429 _display_name=myringtone.mp3 is_alarm=true is_ringtone=true _size=0 artist_id=6 is_music=false _data=/sdcard/MyRingTones/myringtone.mp3
07-14 18:07:09.569: E/Database(7505): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
07-14 18:07:09.569: E/Database(7505):   at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
07-14 18:07:09.569: E/Database(7505):   at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
07-14 18:07:09.569: E/Database(7505):   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
07-14 18:07:09.569: E/Database(7505):   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
07-14 18:07:09.569: E/Database(7505):   at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2519)
07-14 18:07:09.569: E/Database(7505):   at com.android.providers.media.MediaProvider.insert(MediaProvider.java:2341)
07-14 18:07:09.569: E/Database(7505):   at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
07-14 18:07:09.569: E/Database(7505):   at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
07-14 18:07:09.569: E/Database(7505):   at android.os.Binder.execTransact(Binder.java:320)
07-14 18:07:09.569: E/Database(7505):   at dalvik.system.NativeStart.run(Native Method)

请帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

I solve problem.


 Uri uri = MediaStore.Audio.Media.getContentUriForPath(SDCardRoot);                                                               
                               getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + SDCardRoot + "\"", null);                               
                               Uri newUri = getContentResolver().insert(uri, values);