2 years ago
#295147
Kleber Mota
parameter Intent from onActivityResult receiving null value
I am trying implement the basic example from here. It works fine in the first version, when I had only this 2 methods:
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
and
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
}
}
but when I add the methods private fun createImageFile(): File and private fun galleryAddPic() described in the tutorial, and change the method dispatchTakePictureIntent to this:
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
println(ex.stackTrace)
null
}
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(this, "org.kleber.camera.fileprovider", it)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
println(e.stackTrace)
}
}
}
}
}
I am getting this error:
2022-03-15 17:16:40.946 11151-11151/org.kleber.camera E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.kleber.camera, PID: 11151
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {org.kleber.camera/org.kleber.camera.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
at android.app.ActivityThread.deliverResults(ActivityThread.java:5579)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5620)
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:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
at org.kleber.camera.MainActivity.onActivityResult(MainActivity.kt:66)
at android.app.Activity.dispatchActivityResult(Activity.java:8550)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5572)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5620)
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:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
Anyone can see what I am possibly doing wrong here?
android
kotlin
camera
onactivityresult
0 Answers
Your Answer