capturing images with MediaStore.ACTION_IMAGE_CAPTURE intent in android
Solution 1
Photos taken by the ACTION_IMAGE_CAPTURE are not registered in the MediaStore automatically on all devices.
The official Android guide gives that example: http://developer.android.com/guide/topics/media/camera.html#intent-receive But that does not work either on all devices.
The only reliable method I am aware of consists in saving the path to the picture in a local variable. Beware that your app may get killed while in background (while the camera app is running), so you must save the path during onSaveInstanceState.
Edit after comment:
Create a temporary file name where the photo will be stored when starting the intent.
File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, PICTURE_REQUEST_CODE);
fileName
is a String, a field of your activity. You must save it that way:
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", fileName);
}
and recover it in onCreate():
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
fileName = savedInstanceState.getString("fileName");
// ...
}
Now, at the time of onActivityResult, you know the name of the file where the photo was stored (fileName
). You can do anything you wish with it, and then delete it.
2013-09-19 edit: It seems that some camera apps ignore the putExtra uri, and store the pic in a different place. So, before using the value of fileName
you should check whether the intent is null or not. If you get a non-null intent, then you should prefer intent.getData()
over fileName
. Use fileName
as a backup solution only when it is needed.
Solution 2
My onActivityResult() method looks like this and it works in obtaining the image from MediaStore.ACTION_IMAGE_CAPTURE:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
}
}
sathish
Updated on February 06, 2020Comments
-
sathish over 4 years
I am capturing images using MediaStore.ACTION_IMAGE_CAPTURE intent. it is working fine in most of the devices. but it is not working correctly in some latest android device as expected.
my intention is capture image using camera and send it to the server but not to store that image in default gallery of the device.
**: When i capture image, it is returning some other gallery image in onActivityResult method instead of captured image in some latest android devices. I am using below code to capture and store images.
public void launchCamera(View v) { Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera, CAMERA_PIC_REQUEST ); }
In onActivityResult method,
String[] projection = { MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.DATA, BaseColumns._ID, }; Cursor c = null; Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; try { if (u != null) { c = managedQuery(u, projection, null, null, null); } if ((c != null) && (c.moveToLast())) { Bitmap thumbnail = getBitMapFromLocalPath(c.getString(2), 3); idsImagesgot.add(thumbnail); ContentResolver cr = getContentResolver(); cr.delete( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=" + c.getString(3), null); } } finally { if (c != null) { c.close(); } }
Can any one help me out in this regards.
Thanks in advance.
Sathish
-
sathish over 11 yearsThanks for your reply Remi, Can you please guide me any feasible way that meets my requirements i stated above, if possible?
-
sathish over 11 yearsHi Remi, thanks for your code. i am using the same above and providing URI to the capture intent and after saving my image into server i am deleting that temparory image but the image is still available in default gallery.. can you pls suggest me the way i need to delete file from default gallery.
-
Rémi over 11 yearsHow do you delete the file? Something like
getContentResolver().delete(uri, null, null);
should work, I suppose. -
Rémi over 11 yearsAlso, this may be useful: stackoverflow.com/questions/6390163/…
-
MegaX over 10 yearsThis will return a thumbnail, not the real image.
-
IgorGanapolsky over 10 years@Markust Look at the user's original question carefully. In his code he is intending to obtain the thumbnail.
-
A.J. Uppal about 10 yearsWhat is
PICTURE_REQUEST_CODE
in the sixth line of the first paragraph of code? -
Rémi about 10 yearsPICTURE_REQUEST_CODE is a user-defined constant. You can define it to any value you like:
private static final int PICTURE_REQUEST_CODE = 1;
. -
rockhammer over 7 years@Igor, despite what OP's intentions are, might you be able to show a solution that returns the full size image? That would be very helpful indeed. thx