2012-10-09 19 views
8

在我的應用程序,多次接觸連接到單一的.vcf文件併發送至郵箱該文件,嘗試這個日誌貓的所有聯繫人的數據顯示,但無法發送附單.vcf文件的所有數據?Android如何發送多個聯繫人附加在單個.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"));    
} 
}  

回答

6

最後我的問題是使用這樣

 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, "[email protected]");   
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation.getAbsolutePath())); 
    startActivity(Intent.createChooser(sharingIntent, "Send email"));    
} 
+0

我沒有看到你一直在初始化'filedata'。 – BBdev

+0

@BBdev看編輯碼小不匹配。 – NagarjunaReddy

+0

我只是偶然在此,所以我來到這裏我已經固定我的問題,但認爲對一些人這將是告訴你:)有用。只是爲了未來的迴應。謝謝 – BBdev

2

我成功地測試了以下內容。您的代碼更改以「CHANGE:」開頭的註釋顯示。不要忘記清單中有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"));    
    } 

} 
+0

它不工作發送郵件文件解決的問題是0K只爲回答我的問題解決更新了我的答案即將謝謝..... – NagarjunaReddy

+0

我的解決辦法爲我工作 - 當我運行它,我得到提示應用程序的列表,使用郵寄.vcf文件,當我在列表中選擇其中的應用程序,40 KB的.vcf文件附加在新信息。我成功地將文件發送給自己,並確認我的聯繫人已在其中。很高興你有一個解決方案,但我很抱歉我沒有爲你工作。如果您要提供詳細信息,我很想知道我的解決方案失敗的位置。謝謝。 – hBrent

+0

看到了它的工作好過時的答案.... – NagarjunaReddy

相關問題