使用onActivityResult将图像附加到电子邮件

时间:2014-02-04 08:13:15

标签: android email android-intent

之前我确实从图库中选择图片并附加到并发送电子邮件,请参阅here

但是这次我允许用户使用内置摄像头拍摄图像,然后想要将拍摄的图像附加到电子邮件中......

我已为两者编写代码,捕获图像并发送电子邮件,请参阅下面的代码: public class CameraActivity extends Activity实现MediaScannerConnectionClient {

  private File myImageFile = null;
  private Uri myPicture = null;
  private MediaScannerConnection conn = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        ImageButton buttonCaptureImage = (ImageButton) findViewById(R.id.btnCaptureImage);
        buttonCaptureImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                captureImage();
            }
        });
    }

    public void captureImage()
    {
        myImageFile = new File(Environment.getExternalStorageDirectory()+"/CapturedImage.jpg");
        myImageFile.delete();
        myPicture = Uri.fromFile(myImageFile);
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);
        startActivity(i);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==Activity.RESULT_OK)
        {
          startScan();
        }
    }


 // to attach an image to email
    public void sendImage() {
        // TODO Auto-generated method stub
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT, "body of email");
        startActivity(Intent.createChooser(i,"Send email via:"));   
    }
    private void startScan()
    {
        if(conn !=null)
        {
            conn.disconnect();
        }

        if(myImageFile.isFile()) {
            conn = new MediaScannerConnection(this, this);
            conn.connect();
        }
        else {
            Toast.makeText(this,
                "Image does not exist ?",
                Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onMediaScannerConnected() {
        conn.scanFile(myImageFile.getPath(), null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                sendImage();
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }

将图像附加到onActivityResult(...)中的电子邮件我在onScanCompleted(....)中使用以下方法但仍未解决

   // to attach an image to email
    public void sendImage() {
        // TODO Auto-generated method stub
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT, "body of email");
        startActivity(Intent.createChooser(i,"Send email via:"));   
    }

3 个答案:

答案 0 :(得分:1)

你需要将onIcanCompleted的sendImage代码放在这样:

@Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                sendImage();
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }

为了更好地理解,请查看here

答案 1 :(得分:0)

将图片附加到电子邮件

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image_Path"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

答案 2 :(得分:0)

当我想与文本共享Image时,我设置内容类型image / *,我也从文件描述符创建Uri对象而不是路径字符串。

Uri imageFileUri = Uri.fromFile(imageFile); //file descriptor should be valid

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Some text");
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
shareIntent.putExtra(Intent.EXTRA_STREAM, imageFileUri);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject");
startActivity(shareIntent);

它对我来说总是有用