0

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

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

当代码返回到我的程序(在 onActivityResult 中)时,记录不会存储,并且 log cat 显示错误。这只发生在带有 android 2.3 版本的 Sony XPERIA 上。任何帮助将不胜感激。以下是 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)
4

1 回答 1

0

我可以想到两个解决这个问题的方法。我确信还有其他人。

A. 通过捕获 SQLiteException 并对其执行某些操作(或不执行任何操作)来让插入失败。这涉及将上述步骤 2 中的 db.insert 方法调用替换为 try/catch 块内的 db.insertOrThrow。

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

简单但不太令人满意。


B. 在支持 API 8 或更高版本的 IDE 或设备上,使用 insertWithOnConflict 并明确指定冲突解决方案,而不是 insertOrThrow。

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

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

冲突也可以忽略:

     db.insertWithOnConflict(DBHelper.TABLE_NAME, null, insertVals, SQLiteDatabase.CONFLICT_IGNORE);
于 2013-01-03T06:11:21.760 回答