0

我正在尝试创建一个应用程序,该应用程序在按下按钮时拍照,然后在按下发送按钮时将其附加到电子邮件中。我的代码显示图片已附加到电子邮件中,但是当我检查我发送它的电子邮件时,附件没有发送。图像也没有保存在 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);

}
4

1 回答 1

0

请参阅ACTION_IMAGE_CAPTURE的文档!如果您没有提供 EXTRA_OUTPUT(带有应保存图像的位置的 uri),那么结果您将得到“小尺寸图像作为位图对象返回”。

因此,只需在请求图片时将 uri 添加到 intent 并将相同的 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;
于 2012-05-25T15:25:08.590 回答