Camera Intent returns null onActivityResult
Your preinsert a uri here:
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
So when you get a Activity.RESULT_OK
just load the taken photo by its known url. Then you can set the path onActivityResult
like below but you need to convert in to Bitmap.
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Convert here your uri to bitmap then set it.//
mImageView.setImageBitmap(YOUR_BITMAP);
}
Camera activity returning null android
You are getting wrong because you are doing it wrong way.
If you pass the extra parameter MediaStore.EXTRA_OUTPUT
with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in the onActivityResult
method.
If you will check the path which you are passing then you will know that actually camera had write the captured file in that path.
For further information you can follow this, this and this
Camera Intent: why data coming null?
When the onActivityResult method is calling, the variable data comes as null
That is because it is supposed to be null
. There is not supposed to be any output delivered via onActivityResult()
, other than the result code.
You know where the photo is supposed to be. It is supposed to be in photoFile
. That is where you asked the camera app to put the photo, via EXTRA_OUTPUT
. So, look for it there first. Then, if the photo is not there, see if it was a buggy camera app and returned a Uri
via the Intent
delivered to onActivityResult()
.
Intent is always null when taking a picture
The Image data will not be returned by the intent on the onActivityResult, but rather it will be saved on the file path created (If file creation was successful considering the permission aspect in different android versions)
Step 1. make the photofile a global variable
private var photoFile: File? = null
Step 2. Update your dispath method with the glabal photofile
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
Log.d("ERROR", "An error occured")
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"pim.android.photoapp.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
Step 3. Update your on activity result
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Log.d("DATA", data.toString())
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
photoFile?.let {
val imageBitmap: Bitmap= BitmapFactory.decodeFile(it.absolutePath)
binding.imageView.setImageBitmap(imageBitmap)
}
}
}
Uri returned in camera intent is null
on StackOverflow you would find these topics according to yours:
My Android camera Uri is returning a null value, but the Samsung fix is in place, help?
Camera activity returning null android
Android Camera : data intent returns null
Bitmap always returning null in camera intent
In onActivityResult()
, you have:
if (requestCode == 7 && resultCode == RESULT_OK && data != null &&
data.getData() != null) {
The first three parts of that are fine. However, data.getData() != null
has two problems:
It is not relevant, in that the
if
block does not attempt to usedata.getData()
(though you have commented-out code that does)data.getData()
is supposed to benull
forACTION_IMAGE_CAPTURE
, so for correctly-implemented camera apps, yourif
condition will always befalse
If you remove data.getData() != null
, you should go into the if
block and be able to try to get your Bitmap
.
As others have noted, this code will give you a thumbnail-sized Bitmap
. If you are looking for the camera app to give you a full-resolution image, you will need to use EXTRA_OUTPUT
. This sample app demonstrates how this is done.
Related Topics
How to Implement a Custom Alertdialog View
Running Google Maps V2 on the Android Emulator
Get Selected Item Using Checkbox in Listview
How to Enable Logcat/Console in Eclipse For Android
How to Install Google Frameworks (Play, Accounts, etc.) on a Genymotion Virtual Device
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape
Adding Local .Aar Files to Gradle Build Using "Flatdirs" Is Not Working
Notification Icon with the New Firebase Cloud Messaging System
Deleting a Gallery Image After Camera Intent Photo Taken
How to Inflate One View with a Layout
Location of SQLite Database on the Device
Listen to Volume Buttons in Background Service
How to Pass a Variable from Activity to Fragment, and Pass It Back
Android: Broadcast Receiver for Screen on and Screen Off
Runtimeexception: Unable to Instantiate Application