用相机拍照并将其发送到Android中的邮件

时间:2013-06-07 21:46:05

标签: android

我想编写一个只有这个功能的应用程序: - 使用手机摄像头拍摄图像 - 将其作为邮件附件发送到给定地址

我写了以下代码,我不明白为什么我不能退出相机模式(按下接受按钮没有效果)

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(
                Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES
                ), 
                "BAC/"
            ); 
        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;
    }

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

            @Override
            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
                    e.printStackTrace();
                }



            }
        });
    }

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

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            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();
            }
        }  
    } 
}

我将不胜感激任何建议! 谢谢!

2 个答案:

答案 0 :(得分:2)

由于您的相册目录不存在,相机无法返回。试试

public File getAlbumDir() {
    File storageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "BAC/"
    );

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

    return storageDir;
}

此外,由于您提供了EXTRA_OUTPUT个额外信息,data中的参数onActivityResult()可能 null 。您可以从File对象构建位图

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

答案 1 :(得分:1)

对于遇到相同问题的任何人,更正后的功能代码如下:

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(
                Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES
                ), 
                "BAC/"
            ); 
         // Create directories if needed
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

        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;
    }

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

            @Override
            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
                    e.printStackTrace();
                }



            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  

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

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"raul.pop90@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Prima poza");
            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();
            }
        }  
    } 
}