【发布时间】:2014-02-04 08:13:14
【问题描述】:
之前我从图库中选择图片并附加到电子邮件中,请参阅here
但这次我允许用户使用内置摄像头捕捉图像,然后想将捕捉到的图像附加到电子邮件中......
我已经为两者编写了代码,用于捕获图像和发送电子邮件,请参阅下面的代码: 公共类 CameraActivity 扩展 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:"));
}
【问题讨论】:
-
您是否遇到任何错误,如果出现请从您的 logcat 发布堆栈跟踪?
-
不,我只是想知道,如何将图像附加到电子邮件中,就在拍摄之后......
-
请参考我在这个问题中的回答 [stackoverflow.com/a/19152708/424413][1] [1]: stackoverflow.com/a/19152708/424413
-
@SankarGanesh 我现在编辑了我的问题检查
-
需要将sendImage的代码放在onScanCompleted中
标签: android email android-intent