Android Camera: Onactivityresult() Intent Is Null If It Had Extras

android camera: onActivityResult() intent is null if it had extras

It happens the same to me, if you are providing MediaStore.EXTRA_OUTPUT, then the intent is null, but you will have the photo in the file you provided (Uri.fromFile(f)).

If you don't specify MediaStore.EXTRA_OUTPUT then you will have an intent which contains the uri from the file where the camera has saved the photo.

Don't know if it as a bug, but it works that way.

EDIT: So in onActivityResult() you no longer need to check for data if null. The following worked with me:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE_REQUEST://actionCode
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
//For Image Gallery
}
return;

case CAPTURE_IMAGE_REQUEST://actionCode
if (resultCode == RESULT_OK) {
//For CAMERA
//You can use image PATH that you already created its file by the intent that launched the CAMERA (MediaStore.EXTRA_OUTPUT)
return;
}
}
}

Hope it helps

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);
}

Android Camera: intent in onActivityResult is always null

You can Try this

if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK)
{
Bundle extras2 = data.getExtras();
if (extras2 != null) {
// do your stuff here
}
else {
// handle this case as well if data.getExtras() is null
Uri selectedImage = data.getData();
}
}

Hope it helps you

Android Camera Intent onActivityResult() Null error using a Developer.Android guide

the variable mImageView is null, you can set reference with findViewById(R.id.idImageView);

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)
}


}
}

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



Related Topics



Leave a reply



Submit