我正在学习 android 并开发一个应用程序来拍照并通过电子邮件发送它们。我在下面的代码中获得了 ImageView 中的图片,但不确定如何将此图片作为电子邮件附件发送,而不将图片保存到设备上的文件中。
理想情况下,我想知道这是否可能?如果是的话,你能给我指出如何实现相同的正确方向吗?另外(可选)如果可以压缩图片。
public class EmailPic extends Activity implements OnClickListener{
ImageButton ibEmail;
Button bEmail;
ImageView ivEmail;
Intent intentEmail;
Bitmap bmpEmail;
final static int picData = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic_email);
initializeVars();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmpEmail = BitmapFactory.decodeStream(is);
}
private void initializeVars() {
ibEmail = (ImageButton)findViewById(R.id.ibTakePicEmail) ;
ivEmail = (ImageView)findViewById(R.id.ivPicEmail);
bEmail = (Button) findViewById(R.id.bSendPicEmail);
bEmail.setOnClickListener(this);
ibEmail.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePicEmail:
intentEmail = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentEmail,picData);
break;
case R.id.bSendPicEmail:
String message = "email Body";
String[] recipients = new String[]{"mymail.com"};
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,message);
startActivity(emailIntent);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle ext = data.getExtras();
bmpEmail = (Bitmap)ext.get("data");
//Log.d("StatusActivity","bmpEmail >>"+bmpEmail);
ivEmail.setImageBitmap(bmpEmail);
}
}
}