Android: save sound as ringtone / SQLiteConstraintException

跟風遠走 提交于 2019-12-19 09:58:44

问题


I'm trying to save a sound as a ringtone in Android using this code. It works like a charm but will fail if I try to save a ringtone that has already been inserted.

A "SQLiteConstraintException" occurs while saving but I'm not able to catch the exception, in fact, I can't catch any execption.

Here's the part of my code:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

try {
  this.getContentResolver().insert(uri, values);
} catch (SQLiteConstraintException e) {
  Log.e("error", e.getMessage());
} catch (SQLiteException e) {
  Log.e("error", e.getMessage());
} catch (Exception e) {
  Log.e("error", e.getMessage());
}

So I'm trying to actually catch any possible exception, but none is caught.

This is the LogCat:

10-12 18:32:51.627: ERROR/Database(217): Error inserting album_id=-1 title=Applause is_notification=true title_key=%,%J%J%B%,%T%P%4% mime_type=audio/ogg date_added=1286908371 _display_name=Applause.ogg is_alarm=true is_ringtone=true artist_id=1 is_music=false _data=/mnt/sdcard/media/audio/ringtones/Applause.ogg
10-12 18:32:51.627: ERROR/Database(217): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
10-12 18:32:51.627: ERROR/Database(217):     at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
10-12 18:32:51.627: ERROR/Database(217):     at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
10-12 18:32:51.627: ERROR/Database(217):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
10-12 18:32:51.627: ERROR/Database(217):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
10-12 18:32:51.627: ERROR/Database(217):     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:1813)
10-12 18:32:51.627: ERROR/Database(217):     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1638)
10-12 18:32:51.627: ERROR/Database(217):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
10-12 18:32:51.627: ERROR/Database(217):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
10-12 18:32:51.627: ERROR/Database(217):     at android.os.Binder.execTransact(Binder.java:288)
10-12 18:32:51.627: ERROR/Database(217):     at dalvik.system.NativeStart.run(Native Method)

I found a workaround: I check if the soundfile exists before I call the .insert-method (and don't call it, if the file's already there), but I'd really like to understand why no exception is caught. Hope anybody can help me out.

Kind regards, Select0r


回答1:


It is not caught because the exception is not in your code. It is not even in your process. It is in the process of the MediaStore content provider.




回答2:


I was having the same issue....it's because you don't have this permission in your manifest

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

Also, if you're using that tutorial, make sure you have this in your manifest as well...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I spent a good half an hour or so trying to figure out why the code wasn't working. I actually removed the try/catch just to see what type of error it would throw in the logcat, and it told me I was missing those permissions.



来源:https://stackoverflow.com/questions/3918034/android-save-sound-as-ringtone-sqliteconstraintexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!