English 中文(简体)
Android: 保存从 App 中拍摄的照片并将其附加到电子邮件上
原标题:Android: Saving a picture taken from an App and attaching it to email

我正在尝试创建一个 App 程序, 该程序在按下按钮时拍摄图片, 然后在按下发送按钮时将其附加到电子邮件上。 我有的代码显示该图片附在电子邮件上, 但附件没有在检查我发送的电子邮件时发送 。 图像也没有保存在 SD 卡中 。

I call the initialize function in my onCreate() to initialize variable.Also I dont know where to put the SaveImage() function which has to save the image once it s taken. I put it in the onActivityResult() but does not work. Thanks in advance!

private void initialize() {
    // TODO Auto-generated method stub
    camera = (Button) findViewById(R.id.Picture);
    sendEmail = (Button) findViewById(R.id.SendMessage);
    iv = (ImageView) findViewById(R.id.ImageReturn);
    MessageTyped = (EditText) findViewById(R.id.MessageField);
    sendEmail.setOnClickListener(this);
    camera.setOnClickListener(this);

    pngDir = new File(Environment.getExternalStorageDirectory(),
    // Loose convention inferred from app examples
            "My Images");

    if (!pngDir.exists())
        pngDir.mkdirs();

}

@Override
public void onClick(View v) {

    // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.Picture:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        SaveImage();//<------------------WHERE DOES THIS GO?

        startActivityForResult(i, cameraData);

        break;

    case R.id.SendMessage:

        EditTextToString();

        EmailIntent = new Intent(android.content.Intent.ACTION_SEND);

        EmailIntent.putExtra(Intent.EXTRA_EMAIL,
                new String[] { "[email protected]" });
        EmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                MessageToBeReceived);

        // EmailIntent.setType("message/rfc822");
        EmailIntent.setType("image/jpeg");
        EmailIntent.putExtra(Intent.EXTRA_STREAM, pngUri);

        startActivity(Intent.createChooser(EmailIntent,
                "Choose an Email client :"));
        break;

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);

    }
}

private void EditTextToString() {
    MessageToBeReceived = MessageTyped.getText().toString();
}

private void SaveImage() {

    File pngFile = new File(pngDir, "jetsam.jpeg");
    // Save file encoded as PNG
    pngUri = Uri.fromFile(pngFile);

}
最佳回答