java - kotlin.TypeCastException: null 不能转换为非 null 类型 android.graphics.Bitmap
问题描述
我正在进行调整,而不是打开相机拍摄画廊的图像,但我遇到了这个错误
解决了 - - - - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- ------------> val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap 行错误
日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: map.app, PID: 7359
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:3, request=1000, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/48/ORIGINAL/NONE/1141690198 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F48/ORIGINAL/NONE/1141690198} }} to activity {map.app/map.app.MainActivity}: kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap
at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap
at map.app.fragments.ReportFragment.onActivityResult(ReportFragment.kt:272)
at android.app.Activity.dispatchActivityResult(Activity.java:7303)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
代码
fun openCamera() {
try {
val imageFile = createImageFile()
val callCameraIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if(callCameraIntent.resolveActivity(activity.packageManager) != null) {
val authorities = activity.packageName + ".fileprovider"
this.imageUri = FileProvider.getUriForFile(context, authorities, imageFile)
callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(callCameraIntent, IMAGE_PICK_CODE)
}
} catch (e: IOException) {
Toast.makeText(context, "Could not create file!", Toast.LENGTH_SHORT).show()
}
}
部分错误代码 line error at line val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap 原因:kotlin.TypeCastException:null 不能转换为非 null 类型 android.graphics.Bitmap
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {
try {
//Getting the Bitmap from Gallery
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
this.imgThumb!!.setImageBitmap(bitmap)
this.pictureTaken = true
} catch (e:IOException) {
e.printStackTrace()
}
} else {
Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
}
}
@Throws(IOException::class)
fun createImageFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imageFileName: String = "JPEG_" + timeStamp + "_"
val storageDir: File = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
if(!storageDir.exists()) storageDir.mkdirs()
val imageFile = File.createTempFile(imageFileName, ".jpg", storageDir)
imageFilePath = imageFile.absolutePath
return imageFile
}
解决方案
您必须将其转换为可为空的类型而不是不可为空的类型。请执行下列操作:
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
请注意,Bitmap?
而不是Bitmap
推荐阅读
- html - 为什么ajax在html模板中返回循环的最新变量?
- javascript - Sequelize 包含嵌套列:“where 子句”中的未知列
- firebase - Dart firebase 和等价物
- batch-file - 在批处理文件中使用完整路径或相对路径
- azure - Azure:如何找到容器注册表的所有者或资源 ID?
- snowflake-cloud-data-platform - 我如何从多个表中复制到?雪花
- heroku - Heroku错误?说我的 package.json 缺少东西
- firebase - 如何在一个查询中结合“where”、“starAt”和“limit” [FIREBASE,REACT NATIVE,REDUX]
- node.js - 从 axios 设置值到外面
- pandas - 如何使用 for 循环遍历多个 panda 数据帧