0

I want to add an image attachment to email from my Gallery and send with texts. This is my code with reference by(Email with attachment):

public class SendEmail extends Activity {

Button btnsend;
EditText txtreceiver;
EditText txtsubject;
EditText txtinfo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send_email);

    btnsend = (Button) findViewById(R.id.btnsend);
    txtreceiver = (EditText) findViewById(R.id.txtreceiver);
    txtsubject = (EditText) findViewById(R.id.txtsubject);
    txtinfo = (EditText) findViewById(R.id.txtinfo);

          btnsend.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {

          String to = txtreceiver.getText().toString();
          String subject = txtsubject.getText().toString();
          String message = txtinfo.getText().toString();

          Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, message);

          File root = Environment.getExternalStorageDirectory();
          String pathToMyAttachedFile="temp/attachement.xml";
          File file = new File(root, pathToMyAttachedFile);
          if (!file.exists() || !file.canRead()) {
              return;
          }
          Uri uri = Uri.fromFile(file);
          email.putExtra(Intent.EXTRA_STREAM, uri);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client:"));
    }
        });
    }
}

If i remove this part:

File root = Environment.getExternalStorageDirectory();
      String pathToMyAttachedFile="temp/attachement.xml";
      File file = new File(root, pathToMyAttachedFile);
      if (!file.exists() || !file.canRead()) {
          return;
      }
      Uri uri = Uri.fromFile(file);
      email.putExtra(Intent.EXTRA_STREAM, uri);

I can send email, but then it is only text with no attachment. Is my implementation wrong? How i can correct it. Thank you for helps.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52

0 Answers0