Ravul Ravul - 9 days ago 4x
Android Question

Taking picture with camera and sending it to mail in Android

I want to write an application that only has this functionality:
- take image using phone camera
- send it as a mail attachment to a given address

I have written the following code, and i don't understand why i cant exit camera mode (pressing the accept button has no effect)

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private File f;
public File getAlbumDir()

File storageDir = new File(
return storageDir;
private File createImageFile() throws IOException {
// Create an image file name

String imageFileName =getAlbumDir().toString() +"/image.jpg";
File image = new File(imageFileName);
return image;

public void onCreate(Bundle savedInstanceState) {
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
try {
f = createImageFile();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (IOException e) {
// TODO Auto-generated catch block


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"first.last@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "first picture");
i.putExtra(Intent.EXTRA_TEXT , "body of email");

Uri uri = Uri.fromFile(f);
i.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();

I would appreciate any suggestions!


The camera does not return because your album directory does not exists. Try with

public File getAlbumDir() {
    File storageDir = new File(

    // Create directories if needed
    if (!storageDir.exists()) {

    return storageDir;

Also, since you provide an EXTRA_OUTPUT extra, the parameter data might be null in onActivityResult(). You can build the bitmap from your File object instead

Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());