tags:

views:

149

answers:

2

Hi

I need to push an intent to default camera application to make it take a photo, save it and return an URI. Is there any way to do this?

+1  A: 

Try the following I found here http://www.damonkohler.com/2009/02/android-recipes.html

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
// ...

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...
  }
}
Ryan
Thanks but taken picture isn't saving to a device so I'm getting FileNotFoundException in Uri uri = Uri.parse(data.toURI()); bitmap = android.provider.MediaStore.Images.Media .getBitmap(contentResolver, uri);
Aleksander O
Are you saying the picture you take isn't being stored on the phone/device by your choice or something is happening and even though you tell it to store the image on your phone/device it acts like it's not saving?
Ryan
I'm saying that picture isn't being stored on device automatically but returns as a bitmap in onActivityResult(). However I've found a solution which I'll mention in an answer.
Aleksander O
+2  A: 
private Uri imageUri;

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }
}
Aleksander O