Android 如何发送多个联系人附加在单个 .vcf 文件中并发送到邮件?

Posted

技术标签:

【中文标题】Android 如何发送多个联系人附加在单个 .vcf 文件中并发送到邮件?【英文标题】:Android how to send multiple contacts are attached in single .vcf file and send to mail? 【发布时间】:2012-09-29 15:56:53 【问题描述】:

在我的应用程序中,多个联系人附加到单个 .vcf 文件并将该文件发送到邮件,尝试将所有联系人数据显示在 log cat 中,但无法发送所有数据附加到单个 .vcf 文件?

任何人对此有任何想法,请帮助我。

这是我的代码

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

    vCard = new ArrayList<String>();                 

    Log.i("TAG one", "vfile" +vfile);
    vfile = "Contacts" + "_" + System.currentTimeMillis() + ".vcf";  

    /**
     * This Function For Vcard And here i take one Array List in Which i
     * store every Vcard String of Every Contact Here i take one Cursor and
     * this cursor is not null and its count>0 than i repeat one loop up to
     * cursor.getcount() means Up to number of phone contacts. And in Every
     * Loop i can make vcard string and store in Array list which i declared
     * as a Global. And in Every Loop i move cursor next and print log in
     * logcat.
     * */
    getVcardString();           
       

private void getVcardString()  

    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    Log.i("TAG two", "cursor" +cursor);
    if (cursor != null && cursor.getCount() > 0) 
        cursor.moveToFirst();
        Log.i("Number of contacts", "cursorCount" +cursor.getCount());          

        for (int i = 0; i < cursor.getCount(); i++)                        
            get(cursor);                    
            Log.i("TAG send contacts",  "Contact " + (i + 1) + "VcF String is" +  vCard.get(i));                     
            cursor.moveToNext();                                        
                   

        StringBuffer s = new StringBuffer();
        s.append( vCard.toString());                
        string = s.toString();                           
        file = new File(string);        

    //  Log.i("s", ""+s);   
    //  Log.i("string", ""+string); 
        Log.i("file", ""+file);             

     else 
        Log.i("TAG", "No Contacts in Your Phone");
            
       

public void get(Cursor cursor) 

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

    try 
        AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");          

        FileInputStream fis = fd.createInputStream();
        byte[] buf = new byte[(int) fd.getDeclaredLength()];
        fis.read(buf);
        String vcardstring= new String(buf);          

        String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
        FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, true);
        mFileOutputStream.write(vcardstring.toString().getBytes());

        vCard.add(storage_path);            

     catch (Exception e1)              
        e1.printStackTrace();
    
       

private void data()        

    File filelocation = file;     
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("vnd.android.cursor.dir/email");      
    sharingIntent.setType("application/x-vcard");             
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation));
    startActivity(Intent.createChooser(sharingIntent, "Send email"));            

    

【问题讨论】:

【参考方案1】:

最后我的问题是这样解决的

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

     mContext = this; 

     button = (Button) findViewById(R.id.send);          
     button.setOnClickListener(new OnClickListener()           
        public void onClick(View v)         
            data();
        
    );                      

    /**
     * This Function For Vcard And here i take one Array List in Which i
     * store every Vcard String of Every Contact Here i take one Cursor and
     * this cursor is not null and its count>0 than i repeat one loop up to
     * cursor.getcount() means Up to number of phone contacts. And in Every
     * Loop i can make vcard string and store in Array list which i declared
     * as a Global. And in Every Loop i move cursor next and print log in
     * logcat.
     * */
    getVcardString();           
       

 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");          


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"));            
   

【讨论】:

我没有看到你在哪里初始化filedata 我只是偶然发现了这一点,所以我到了这里我已经解决了我的问题,但认为对于其他一些人来说,告诉你会很有用:)。只是为了将来的反应。谢谢 我是通过其他方式完成的。我的要求不同.:) 但对其他人有用。所以给你+1。 :) 让我们continue this discussion in chat【参考方案2】:

我成功测试了以下内容。以“CHANGE:”开头的 cmets 表示对代码的更改。不要忘记在清单中添加android.permission.WRITE_EXTERNAL_STORAGE

public class MainActivity extends Activity 
    private String vfile;
    private Cursor cursor;
    private ArrayList<String> vCard;
    private String string;
    private File file;
    private String storage_path; //CHANGE:  storage_path global

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vCard = new ArrayList<String>();                 

        Log.i("TAG one", "vfile" + vfile);
        vfile = "Contacts" + "_" + System.currentTimeMillis() + ".vcf";  

    /**
     * This Function For Vcard And here i take one Array List in Which i
     * store every Vcard String of Every Contact Here i take one Cursor and
     * this cursor is not null and its count>0 than i repeat one loop up to
     * cursor.getcount() means Up to number of phone contacts. And in Every
     * Loop i can make vcard string and store in Array list which i declared
     * as a Global. And in Every Loop i move cursor next and print log in
     * logcat.
     * */
        getVcardString();    
        data(); //CHANGE: now calling data to send vCard intent
    

    private void getVcardString()  

       cursor =  getContentResolver().
       query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
       null, null, null, null);
       Log.i("TAG two", "cursor" +cursor);
       if (cursor != null && cursor.getCount() > 0) 
       cursor.moveToFirst();
       Log.i("Number of contacts", "cursorCount" +cursor.getCount());          

       for (int i = 0; i < cursor.getCount(); i++)                        
            get(cursor);                    
            Log.i("TAG send contacts",  
            "Contact " + (i + 1) + "VcF String is" +  vCard.get(i));                     
            cursor.moveToNext();                                        
                   

//            StringBuffer s = new StringBuffer(); CHANGE: not needed
//            s.append( vCard.toString());                
//            string = s.toString();                           
//            file = new File(string); //CHANGE: this is not what file should be        

         else 
            Log.i("TAG", "No Contacts in Your Phone");
                
           

    public void get(Cursor cursor) 

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

        try 
            AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");          

            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String vcardstring= new String(buf);     
            vCard.add(vcardstring); //CHANGE: added this, so log statement in above method doesn't generate error

//            String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
            //CHANGE: this is the path we need to get file for intent:
            storage_path = getExternalFilesDir(null).toString() + File.separator + vfile;            
            Log.i("MainActivity", storage_path);
            //CHANGE: this will create the file we need:
            file = new File(getExternalFilesDir(null), vfile);
            file.createNewFile(); //CHANGE
            //CHANGE: this will create the stream we need:
            FileOutputStream mFileOutputStream = new FileOutputStream(file, true); 
            mFileOutputStream.write(vcardstring.toString().getBytes());
            mFileOutputStream.close(); //don't forget to close

    //            vCard.add(storage_path); //CHANGE: not needed           

         catch (Exception e1)              
            e1.printStackTrace();
        
           

    private void data()      
    //        File filelocation = file; //CHANGE: not what we need    
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("vnd.android.cursor.dir/email");      
        sharingIntent.setType("application/x-vcard"); 
        //CHANGE: using correct path:
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(storage_path));
        startActivity(Intent.createChooser(sharingIntent, "Send email"));            
    


【讨论】:

它不起作用发送邮件文件仅为 0k 感谢您的回答我的问题已解决很快更新我的答案..... 我的解决方案适用于我——当我运行它时,系统会提示我提供用于邮寄 .vcf 文件的应用程序列表,当我选择列表中的一个应用程序时,新消息中附加了 40 KB .vcf 文件。我成功地将文件发送给自己,并确认我的联系人在其中。很高兴你有一个解决方案,虽然我很抱歉我的没有为你工作。如果您能提供详细信息,我很想知道我的解决方案在哪里失败。谢谢。

以上是关于Android 如何发送多个联系人附加在单个 .vcf 文件中并发送到邮件?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过长按我的 Android 应用程序的联系人列表中的联系人添加附加选项?

在 Android 中以编程方式创建的多个文本视图的单个 onclick 侦听器

如何将多个值附加到 html 表单中的单个参数?

如何将多个表中的电话号码插入mysql中的单个联系人表?

如何从android中的单个查询中获取联系人的名字,姓氏,电子邮件ID

如何在通过 Pandas 在 csv 中写入多个 for 循环的数据时在单个单元格中附加数据?