Android:将声音保存为铃声/SQLiteConstraintException

发布于 2024-09-27 07:45:41 字数 2491 浏览 3 评论 0原文

我正在尝试使用 代码。它的工作原理就像一个魅力,但如果我尝试保存已插入的铃声,它就会失败。

保存时发生“SQLiteConstraintException”,但我无法捕获异常,事实上,我无法捕获任何执行。

这是我的代码的一部分:

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());
}

所以我试图实际捕获任何可能的异常,但没有捕获到。

这是 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)

我找到了一个解决方法:在调用 .insert-method 之前检查声音文件是否存在(如果文件已经存在,则不要调用它),但我真的很想了解为什么没有例外被抓住了。希望任何人都可以帮助我。

亲切的问候, 选择0r

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-10-04 07:45:41

它没有被捕获,因为异常不在您的代码中。它甚至不在你的进程中。它处于 MediaStore 内容提供商的进程中。

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.

别理我 2024-10-04 07:45:41

我遇到了同样的问题......这是因为你的清单中没有此权限

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

另外,如果你正在使用该教程,请确保你的清单中也有这个......

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

我花了一半的时间大约一个小时试图找出代码不起作用的原因。实际上,我删除了 try/catch 只是为了看看它会在 logcat 中抛出什么类型的错误,它告诉我我缺少这些权限。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文