Android录音机:android.database.sqlite.SQLiteConstraintException:错误代码19

时间:2013-01-03 04:52:11

标签: android android-sqlite android-mediarecorder

我正在使用以下意图录制声音

Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, REQUEST_RECORD_SOUND);

当代码返回我的程序时(在onActivityResult中),不存储记录并且log cat显示错误。这只发生在索尼XPERIA和Android 2.3版本上。 任何帮助将受到高度赞赏。 以下是logcat的输出:

01-02 21:43:17.837: E/Database(557): Error inserting album_id=8 title=2013-01-02 21:43:17 title_key=Q*?Q*?Q*?Q*?Q?Q*?Q*?Q?Q*?Q*?QQ*?Q*?QØQ*?Q*?QØQ*?Q*?Q mime_type=audio/3gpp _display_name=recording-379099974.3gpp date_modified=1357188193 date_added=1357188197 artist_id=8 is_music=0 _data=/mnt/sdcard/recording-379099974.3gpp
01-02 21:43:17.837: E/Database(557): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
01-02 21:43:17.837: E/Database(557):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
01-02 21:43:17.837: E/Database(557):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
01-02 21:43:17.837: E/Database(557):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1590)
01-02 21:43:17.837: E/Database(557):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1434)
01-02 21:43:17.837: E/Database(557):    at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:1981)
01-02 21:43:17.837: E/Database(557):    at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1800)
01-02 21:43:17.837: E/Database(557):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
01-02 21:43:17.837: E/Database(557):    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
01-02 21:43:17.837: E/Database(557):    at android.os.Binder.execTransact(Binder.java:324)
01-02 21:43:17.837: E/Database(557):    at dalvik.system.NativeStart.run(Native Method)
01-02 21:43:18.057: E/WindowManager(1524): Activity com.android.soundrecorder.SoundRecorder has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2afe6e80 that was originally added here
01-02 21:43:18.057: E/WindowManager(1524): android.view.WindowLeaked: Activity com.android.soundrecorder.SoundRecorder has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2afe6e80 that was originally added here
01-02 21:43:18.057: E/WindowManager(1524):  at android.view.ViewRoot.<init>(ViewRoot.java:267)

1 个答案:

答案 0 :(得分:0)

我可以想到这个问题的两个修复。我相信还有其他人。

一个。通过捕获SQLiteException并对其执行某些操作(或什么都没有)来使插入失败。这涉及使用try / catch块中的db.insertOrThrow替换上面步骤2中的db.insert方法调用。

    try {
    db.insertOrThrow(UpdateServiceDBHelper.TABLE_NAME, null, statusVals);
        } catch (SQLException ex) {
     // do something with ex or nothing
     } 

简单但不太满意。


B中。在IDE或支持API 8或更高版本的设备上,使用insertWithOnConflict而不是insertOrThrow,并显式指定冲突重新构建模式。

此方法调用导致冲突的行被替换。

     db.insertWithOnConflict(DBHelper.TABLE_NAME, null, insertVals, SQLiteDatabase.CONFLICT_REPLACE);

冲突也可以忽略:

     db.insertWithOnConflict(DBHelper.TABLE_NAME, null, insertVals, SQLiteDatabase.CONFLICT_IGNORE);