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,471
  • 2
  • 37
  • 51

1 Answers1

0

excuse me ,try again with a new links: http://www.androidhub4you.com/2013/09/send-email-with-attachment-in-android.html

khouloud mejdoub
  • 966
  • 1
  • 7
  • 14
  • this seems what i am looking for. thanks alot. i will let you know later if it works. – Umit Kaya Dec 27 '13 at 15:35
  • This is a horrible answer, please explain what needs to be done in your own words. The site behind this link might someday go down and other users will have no idea what to do – avalancha Jan 21 '14 at 15:37