Android:保存从应用程序中获取的图片并将其附加到电子邮件中

时间:2012-05-25 14:54:13

标签: android android-camera email-attachments

我正在尝试创建一个按下按钮时拍照的应用程序,然后在按下发送按钮时将其附加到电子邮件中。我的代码显示图片已附加到电子邮件中,但是当我查看我发送给它的电子邮件时,附件没有发送。图像也没有保存在SD卡中。

我在onCreate()中调用初始化函数来初始化变量。我也不知道放置SaveImage()函数的位置,它必须在拍摄后保存图像。我把它放在onActivityResult()中,但不起作用。 提前谢谢!

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[] { "bipush.osti@gmail.com" });
        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);

}

1 个答案:

答案 0 :(得分:0)

请参阅ACTION_IMAGE_CAPTURE的文档!如果你没有给EXTRA_OUTPUT(uri带有保存图像的位置),那么在结果中你会得到“小尺寸图像作为Bitmap对象返回”。

因此,在请求图片时将uri添加到意图中并将相同的uri传递给ACTION_SEND:

case R.id.Picture:
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, getUriForNewFile()); // you have to implement youself getUriForNewFile()

    startActivityForResult(i, cameraData);

    break;