android - Android ContentResolver.insert() 抛出 NullPointerException
问题描述
我正在尝试将一些音频文件(m4a、mp3、...)添加到 MediaStore 的音频收藏中,以便其他音乐应用程序可以列出它们。为了简单起见,我只是复制了文档中的内容。(https://developer.android.com/training/data-storage/shared/media)
所以在 onCreate() 方法的活动中我写了这个:
val resolver = applicationContext.contentResolver
val audioCollection =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Audio.Media.getContentUri(
MediaStore.VOLUME_EXTERNAL_PRIMARY
)
} else {
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
val songDetails = ContentValues().apply {
put(MediaStore.Audio.Media.DISPLAY_NAME, "Test Track")
}
resolver.insert(audioCollection, songDetails)
但是每次我启动它时,我都会得到以下信息:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.lastIndexOf(int)' on a null object reference
at android.os.Parcel.createException(Parcel.java:1972)
at android.os.Parcel.readException(Parcel.java:1934)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1593)
清单声明了 READ、WRITE 以及 MANAGE_EXTERNAL_STORAGE 权限,并在我设备上的 android 应用设置中启用了这些存储权限。
解决方案
推荐阅读
- mysql - 无法从 MySQL 中的标题列创建外键
- sql-server - Excel 工作簿 - 将连接文件共享到 SQL Server
- php - 如何使用代码将图像上传到产品 woocommerce
- monitoring - Prometheus 黑盒探测有用的指标
- python-3.x - 无法为视频帧绘制实时图表?
- c# - 隐藏后的动画窗口
- java - Webdriver:仅在测试成功时关闭 Web 驱动程序
- sql - 甲骨文。应用条件排序。如果 >=2 行有重复数据,则这些行必须遵循与没有重复数据的行不同的条件
- angular - 通过 CKEditor 5 将图像上传到 Express 服务器会导致 Angular 应用程序中出现 Multer 错误
- javascript - fetch 不会让我授权获取请求