Reputation: 1362
there is a simple tutorial to take a picture with android (http://developer.android.com/guide/topics/media/camera.html) which I followed.
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
protected void onTakePicture()
{
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
File file = Environment.getExternalStorageDirectory();
String path = file.getAbsolutePath() + "/Android/data/com.myapplication/files";
File dir = new File(path);
if (!dir.isDirectory())
dir.mkdirs();
File outFile = new File(path + File.separator + "img.jpg");
Uri fileUri = Uri.fromFile(outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
// Image captured and saved to fileUri specified in the Intent
Uri fn = data.getData();
Toast.makeText(this, "Image saved to:\n" + fn.toString(), Toast.LENGTH_LONG).show();
}
}
I am calling this from within an activity. The problem now is that when the camera activity appears, I can take multiple pictures, but I only want to take a single picture and then return to the calling activity. onActivityResult will not be called until I leave the camera activity (by pressing the "back" button). Then the data.getData() crashes.
Any ideas?
Thank you Gerhard
Upvotes: 2
Views: 1856
Reputation: 9052
I know this is an old question but I ran into the same problem.
For me it was fixed by not providing a URI but the URI path myUri.getPath()
The following should do what you want:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri.getPath());
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Upvotes: 0
Reputation: 47779
Hmm ... this works for me to capture the image:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(intent, AbstractActivity.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
when the user takes the picture, it asks whether the user wants to accept this image, and then it goes back to my activity where I ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
handleAvatarUpload(data); // which uses Uri selectedImage = data.getData();
}
}
}
I haven't received any crash reports with this code and it's been out in the wild for a few months. our userbase is in line with other apps in our category with android 2.3.3 at around 71% of users, 2.2 at 16.35%, and 2.1 at 5.7%
Upvotes: 1