如何发送附带在android中的文件的电子邮件

时间:2012-08-21 05:31:31

标签: email android-intent email-attachments

我创建了文件myown.txt,从文件explorer.path查看它是data-> data-> com.contacts-> files-> myown.txt.i想要发送包含联系人详细信息的文件作为我的电子邮件ID的附件 我的代码是这样的:

BTN =(按钮)findViewById(R.id.button1);
                btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {


                    // TODO Auto-generated method stub
                    sendEmail();
                }


            public void sendEmail(){
                String strfile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/sdcard/myown.txt";
                File f=new File(Environment.getExternalStorageState(),strfile);

                 EditText textTo =(EditText)findViewById(R.id.editTextTo);
                 EditText textSubject =(EditText)findViewById(R.id.editTextSubject);
                 TextView tv=(TextView)findViewById(R.id.textView1);
                 if(!textTo.getText().toString().trim().equalsIgnoreCase("")){
                  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                  emailIntent.setType("plain/text");
                  emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ textTo.getText().toString()});
                  emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, textSubject.getText());
                  //emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, tv.getText());
                 emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f));
                  ContactsbackupActivity .this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                }
                else{
                    Toast.makeText(getApplicationContext(), "Please enter an email address..", Toast.LENGTH_LONG).show();
                }

        }
            });




working on android 2.2. i can only get the textview in email  but not the file. so please do let me know how to solve this problem.thanking you

1 个答案:

答案 0 :(得分:0)

嘿,它工作,我不知道怎么样。我在一个系统中开发了程序,我正在另一个系统上执行。配置有一些变化,这就是为什么它显示error.thanx很多。一个伟大的一天