如何使用gmail在android中附加文件

时间:2015-04-06 08:56:00

标签: android

((HomeActivity) getActivity()).contactus
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        sendEmail();
                    }
                });

        ((HomeActivity) getActivity()).attachmentimageview
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(
                                Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);

                        startActivityForResult(
                                Intent.createChooser(intent, "Complete action using"),
                                MY_INTENT_CLICK);
                    }
                });

        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == MY_INTENT_CLICK) {
                if (null == data)
                    return;

                String selectedImagePath;
                Uri selectedImageUri = data.getData();

                // MEDIA GALLERY
                selectedImagePath = ImageFilePath.getPath(
                        getActivity(), selectedImageUri);
                Log.i("Image File Path", "" + selectedImagePath);
//              txta.setText("File Path : \n" + selectedImagePath);
            }
        }
    }

    private void sendEmail() {
        try {


            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            String[] recipients = new String[] { "Enter email" };
            emailIntent
                    .putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
            emailIntent
                    .putExtra(
                            Intent.EXTRA_EMAIL,
                            new String[] { "anilkumar@softageindia.com,danyalozair@gmail.com" });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    "Feedback");

            emailIntent.putExtra(Intent.EXTRA_STREAM, selectedImagePath );

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    Html.fromHtml(""));
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "HI"
                    + "\n\n" + contactustext.getText().toString());
            emailIntent.setType("message/rfc822");
            startActivity(emailIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的代码我想从Sd卡或图库附加文件我正在使用给定的代码我能够从厨房获取路径但是当我点击联系我们按钮然后它同样工作获取文件目录,如果我们不使用附件然后它与文本正常工作请检查哪里出错了以及如何修复它请建议我actully我想发送一些文本和附件发送通过gmail当我点击按钮联系我们它重定向到附件和文本到Gmail然后我们可以发送它。

2 个答案:

答案 0 :(得分:0)

首先,在Activity之外的FragmentonCreate

之外创建此方法
public static void getVcardString() {   

String path = null;     
String vfile = null;

 vfile = "Contacts.vcf";           
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);

phones.moveToFirst();
Log.i("Number of contacts", "cursorCount" +phones.getCount());  
for(int i =0;i<phones.getCount();i++)   {       

     String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
     Log.i("lookupKey", " " +lookupKey);
     Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
     AssetFileDescriptor fd;

     try  {
         fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
         FileInputStream fis = fd.createInputStream();
         byte[] buf = new byte[(int) fd.getDeclaredLength()];
         fis.read(buf);
         String VCard = new String(buf);          

         path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
         FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
         mFileOutputStream.write(VCard.toString().getBytes());    

         phones.moveToNext();               

         filevcf = new File(path);
         Log.i("file", "file" +filevcf);

     }catch(Exception e1) {
         e1.printStackTrace();  
     }
}       
Log.i("TAG", "No Contacts in Your Phone");          
}

并在onCreate内调用,如:

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);     
 getVcardString();           
 }    

现在再次创建一种新方法,将Email发送到onCreate之外,如:

protected void data() {             
File filelocation = filevcf ;     
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");      
sharingIntent.setType("application/x-vcard");       
sharingIntent.putExtra(Intent.EXTRA_EMAIL, "mail@gmail.com" );        
sharingIntent.putExtra(Intent.EXTRA_STREAM,    Uri.parse("file://"+filelocation.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "Send email"));            
}  

并调用发送电子邮件按钮的data()方法onClick,如:

data();

如果您现在遇到任何问题,请告诉我。

答案 1 :(得分:0)

您可以将文件附加为:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");      
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Please find attachment");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"you file's path"));
startActivity(Intent.createChooser(sharingIntent, "Attach using..."));