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:
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.
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 yourACTION_IMAGE_CAPTURE
Intent
, in which case you know where the image should be stored, and do not need to rely upongetData()
, orUse the
data
extra in the responseIntent
, which will be aBitmap
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
Add "View More" at the End of Textview After 3 Lines
Taking Pictures with Camera on Android Programmatically
Android M Camera Intent + Permission Bug
Lazy Download Images into Gridview
Android SQLite Issue - Table ... Has No Column Named
Best Practice for Nested Fragments in Android 4.0, 4.1 (<4.2) Without Using the Support Library
How to Simulate Touch from Background Service with Sendevent or Other Way
In Android Edittext, How to Force Writing Uppercase
How to Detect Whether the Android Phone in Silent Mode Programmatically
Using Jaxb with Google Android
Use Camera Flashlight in Android
How to Set Multiple Alarms Using Alarm Manager in Android
How to Crop an Image in Android
Startactivity() from Broadcastreceiver
Add a Background Image to Shape in Xml Android