拍照然后通过电子邮件发送

时间:2012-08-23 14:25:13

标签: android android-intent android-camera

我正在尝试创建一个应用程序,您可以在其中拍照,然后通过电子邮件发送给某人。目前我可以拍照并将背景设置为这张照片:

public class Camera extends Activity implements View.OnClickListener{


ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo);
    initialize();
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
    bmp = BitmapFactory.decodeStream(is);
}

private void initialize(){
    ib = (ImageButton) findViewById(R.id.ibTakePic);
    b = (Button) findViewById(R.id.bSetWall);
    iv = (ImageView) findViewById(R.id.ivReturnedPic);
    b.setOnClickListener(this);
    ib.setOnClickListener(this);



}

@Override
public void onClick(View v) {
    File mImageFile;
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bSetWall:
        try {
            getApplicationContext().setWallpaper(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    break;
    case R.id.ibTakePic:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, cameraData);
    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);
    }
}




}

我有一个单独的应用程序,我可以接收用户输入并通过电子邮件发送到预定义的地址:

    public void onClick(View v) {
    // TODO Auto-generated method stub
    convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
    String emailaddress[] = { "info@sklep.com", "", };
    String message = emailAdd + name + beginning;

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);

    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity(emailIntent);

}

如何通过电子邮件发送我拍摄的照片?它保存在哪里以及如何访问它以便我可以通过电子邮件发送它?

非常感谢

2 个答案:

答案 0 :(得分:1)

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                File output = new File(dir, "camerascript.png");
                cPath = output.getAbsolutePath();
                Bitmap b =  BitmapFactory.decodeFile(cPath);
                Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
                FileOutputStream fout;
                try{
                    fout = new FileOutputStream(output);
                    out.compress(Bitmap.CompressFormat.PNG, 100, fout);
                    fout.flush();
                    fout.close();
                    b.recycle();
                    out.recycle();
                }catch(Exception e){
                    e.printStackTrace();
                }

        }
    }

之后发送邮件方法

public void sendMail(){
        Log.e("sendMail", "v r in sendMail");

        i = new Intent(Intent.ACTION_SEND);
        i.setType("plain/text");
            i.putExtra(Intent.EXTRA_EMAIL, new String[] {"abc@gmail.com"});
          i.putExtra(Intent.EXTRA_SUBJECT,"subject...");
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cPath));
        i.putExtra(Intent.EXTRA_TEXT, "Body of Email....");
        startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS);

    }

上面的代码将有助于你..

答案 1 :(得分:0)

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
     Uri uri=data.getData();
     sendmail(uri);

}

and 
  private void sendmail(Uri uri)
  {
    i = new Intent(Intent.ACTION_SEND);
    i.setType("plain/text");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"abc@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT,"subject...");
    i.putExtra(Intent.EXTRA_STREAM, uri);
     i.putExtra(Intent.EXTRA_TEXT, "Body of Email....");
   startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS);

  }

它可能有用。