Reputation: 3
I have been trying to follow this tutorial: https://developer.android.com/training/camera/photobasics
I use an imagebutton, so I open the camera and take the photo, but when I accept the picture it just crashes:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bujess, PID: 15270
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.bujess/com.example.bujess.WritingActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
at android.app.ActivityThread.deliverResults(ActivityThread.java:5097)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
at com.example.bujess.WritingActivity.onActivityResult(WritingActivity.kt:61)
at android.app.Activity.dispatchActivityResult(Activity.java:8292)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5090)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
The most suspicious line is "at com.example.bujess.WritingActivity.onActivityResult(WritingActivity.kt:61)". But those are the lines I copied from the guide onActivityResult:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
val imageView = findViewById<ImageView>(R.id.opGallNCam)
imageView?.setImageBitmap(imageBitmap)
galleryAddPic()
}
}
Line 61 being : "val imageBitmap = data?.extras?.get("data") as Bitmap"
opGallNCam is the Id of the ImageButton
I am new to Kotlin. I thank you in advance
Upvotes: 0
Views: 781
Reputation: 2117
To avoid an exception being thrown, one can use a safe cast operator as? that returns null on failure
val imageBitmap = data?.extras?.get("data") as? Bitmap
Have a look at this you will know more about it https://kotlinlang.org/docs/reference/typecasts.html#safe-nullable-cast-operator
Upvotes: 1
Reputation: 40898
java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
The problem is that: data?.extras?.get("data")
returns a null
value, and you are trying to cast this null
into a Bitmap
First data?.extras?.get("data")
won't return you a Bitmap, but you can get a Uri
object first with data?.data
, and then convert it into a Bitmap
as illustrated below, change your onActivityResult
method to:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 10 && resultCode == RESULT_OK) {
val imageUri = data?.data as Uri
var imageBitmap: Bitmap
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
imageBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, imageUri))
} else {
imageBitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
}
//............... rest of your code
Upvotes: 0