2012-10-09 13 views
8

मेरे आवेदन में, कई संपर्क एकल से जुड़े होते हैं .vcf फ़ाइल और वह फ़ाइल मेल पर भेजी गई है, लॉग बिल्ली में सभी संपर्क डेटा डिस्प्ले को आजमाएं, लेकिन एकल .vcf फ़ाइल से जुड़े सभी डेटा भेजने में असमर्थ ?एंड्रॉइड एकाधिक संपर्क भेजने के लिए एकल .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

मैं नहीं देखता कि आप कहां से दायर किया गया है 'दायरता'। – BBdev

+0

@BBdev संपादित कोड छोटे मेल नहीं खाते देखें। – NagarjunaReddy

+0

मैं बस इस पर ठोकर खा गया था इसलिए मैं यहां आया, मैंने अपनी समस्या तय की थी लेकिन कुछ अन्य लोगों के लिए सोचा था कि यह आपको बताने में उपयोगी होगा :)। बस भविष्य की प्रतिक्रिया के लिए। धन्यवाद – BBdev

2

मैं सफलतापूर्वक निम्नलिखित परीक्षण किया है। आपके कोड में परिवर्तन "परिवर्तन:" से शुरू होने वाली टिप्पणियों के साथ इंगित किए जाते हैं। अपने मेनिफेस्ट में 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

यह मेल फाइल भेजने काम नहीं कर रहा तरह का उपयोग कर हल किया जाता है है 0 हजार केवल धन्यवाद जवाब के लिए मेरी समस्या हल हो जाती है मेरा उत्तर जल्द ही अपडेट ..... – NagarjunaReddy

+0

मेरे समाधान मेरे लिए काम करता है - जब मैं इसे चलाता हूं, तो मुझे .vcf फ़ाइल को मेल करने के लिए उपयोग करने के लिए ऐप्स की एक सूची के साथ संकेत मिलता है, और जब मैं सूची में से किसी एक ऐप का चयन करता हूं, तो 40 KB .vcf फ़ाइल को नए में संलग्न किया जाता है संदेश। मैंने सफलतापूर्वक फ़ाइल को स्वयं भेजा, और पुष्टि की कि मेरे संपर्क इसमें थे। खुशी है कि आपके पास समाधान है, हालांकि मुझे खेद है कि मेरा आपके लिए काम नहीं करता है। मैं जानना चाहता हूं कि मेरा समाधान कहां विफल हुआ, अगर आप विवरण प्रदान करेंगे। धन्यवाद। – hBrent

+0

दिनांकित देखें यह अच्छा काम कर रहा है .... – NagarjunaReddy

संबंधित मुद्दे