Camera Activity Returning Null Android

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

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


}
}

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:

  1. It is not relevant, in that the if block does not attempt to use data.getData() (though you have commented-out code that does)

  2. data.getData() is supposed to be null for ACTION_IMAGE_CAPTURE, so for correctly-implemented camera apps, your if condition will always be false

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.

Android data.getData() returns null from CameraActivity for some phones

Uri imageUri = data.getData(); //The trouble is here

There is no requirement for a camera app to return a Uri pointing to the photo, as that is not part of the ACTION_IMAGE_CAPTURE Intent protocol. Either:

  • Supply EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, in which case you know where the image should be stored, and do not need to rely upon getData(), or

  • Use the data extra in the response Intent, which will be a Bitmap of a thumbnail of the image

Your next bug is here:

String realPath = Image.getPath(this, imageUri);

There is no requirement that the image be stored as a file that you can access, unless you provide a path to that location via EXTRA_OUTPUT.

Android camera capture activity returns null Uri

You have to tell the camera, where to save the picture and remeber the uri yourself:

private Uri mMakePhotoUri;

private File createImageFile() {
// return a File object for your image.
}

private void makePhoto() {
try {
File f = createImageFile();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, REQUEST_MAKE_PHOTO);
} catch (IOException e) {
Log.e(TAG, "IO error", e);
Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
}
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}

You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().



Related Topics



Leave a reply



Submit