Reputation: 153
I'm developing an app that needs to take a picture and save it, and show it in the app. It works for Android 4.0 but not for Android 4.2.2 (and I guess for later versions neither).
Here is the code:
Button photoBtn = (Button) inflated.findViewById(R.id.btn_photo);
photoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fileStorageFolder = Environment.getExternalStorageDirectory() + File.separator + "MobiAgenda";
File file = new File(fileStorageFolder + File.separator + "Contact" + id + ".jpg");
Uri imgUri = Uri.fromFile(file);
imgPath = file.getAbsolutePath();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
float scale = getApplicationContext().getResources().getDisplayMetrics().density;
Bitmap thumb = getThumbnailBitmap(imgPath, Math.round(100*scale));
try
{
FileOutputStream out = new FileOutputStream(imgPath);
thumb.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
System.err.println(e.toString());
}
takenPhoto.setImageBitmap(thumb);
takenPhoto.setVisibility(View.VISIBLE);
}
}
Well, as I said before, this code is working on my HTC Desire C. But I have tried it in two different phones running Android 4.2.2 and these are the behaviours I got:
-Droxio B51: The intent opens the camera. Pressing cancel button, the onActivityResult is called back, with result_code = 0 (and the app does nothing --> OK). But when pressing accept button in camera interface, the photo is saved in the phone (different path than given through EXTRA_OUTPUT), but it does not call back onActivityResult.
-Sony Xperia L: The intent opens the camera. After taking the picture, there is no option to cancel or accept (so I guess is accepting it), but it calls onActivityResult with result_code = 0, so the app does nothing. In this case, the picture is not saved in any path of the phone (or at least I coudn't find it).
Why I'm having this two different behaviours? And the most important thing, how can I solve them?
Thanks.
Upvotes: 0
Views: 1137
Reputation: 1007228
Why I'm having this two different behaviours?
ACTION_IMAGE_CAPTURE
is invoking a third-party application to take a picture. There are thousands of camera apps, both pre-installed ones and ones that users install from places like the Play Store. None have to necessarily support ACTION_IMAGE_CAPTURE
, and those that do can have bugs, as you are experiencing.
how can I solve them?
Stop using ACTION_IMAGE_CAPTURE
, and take pictures yourself in your own app. You have no means of forcing developers to write camera apps that handle ACTION_IMAGE_CAPTURE
properly, and you have no means of forcing users to use camera apps that handle ACTION_IMAGE_CAPTURE
properly.
Upvotes: 1