2012-02-20 4 views
0

次のコードを使用して、1人の連絡先のvcfファイルを作成しています。私が望むのは、特定の連絡先をクリックすると、その連絡先のvcfファイルが作成されるということです。選択した連絡先の.vcfファイルを作成する方法

String vfile = "test1.vcf"; 
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 


      cursor.moveToFirst(); 
      String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
     Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
     AssetFileDescriptor fd; 
      try { 
       fd = getContentResolver().openAssetFileDescriptor(uri, "r"); 
       FileInputStream fis = fd.createInputStream(); 
       byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
       fis.read(buf); 
       String VCard = new String(buf); 
       String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; 
       FileOutputStream out = new FileOutputStream(path); 
       out.write(VCard.toString().getBytes()); 
       Log.d("Vcard", VCard); 
      } catch (Exception e1) 
      { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 

     } 

任意の助けをいただければ幸い: は、ここで私が使用していたコードです。

+0

が私を助けて "インテリジェント灰" http://stackoverflow.com/questions/13198933/cant-find-vcf-file-on-sdcard-android-emulator/13199144#comment17969669_13199144 おかげで進歩 –

答えて

1

chanduちょっと間違いなくあなたが 、まずそれが

public void getVcardString() { 
    // TODO Auto-generated method stub 
System.out.println("i m in get vcardstring"); 
ArrayList<string> vCard = new ArrayList<String>(); 
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "="+ShareContactActivity.user_contact_id, null, null); 

     cursor.moveToFirst(); 
     for(int i =0;i<cursor.getCount();i++) 
     { 

      get(cursor); 
      System.out.println("i m in above vcf string"); 
      System.out.println("TAG"+ "Contact "+(i+1)+"VcF String is"+vCard.get(i)); 
      cursor.moveToNext(); 
     } 
} 

public void get(Cursor cursor) 
{ 
    //cursor.moveToFirst(); 
    FileInputStream fis=null; 
    System.out.println("i m in get cursor"); 
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
    AssetFileDescriptor fd; 
    try { 
     fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); 



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

     String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; // vfile is the variable of type string and hold value like vcf file name whatever name u give to this ariable it will save with that name. 
     filelocation=storage_path;     
     System.out.println("this is file location "+Environment.getExternalStorageDirectory().toString()+File.separator+vfile); 
     FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false); 
     mFileOutputStream.write(vcardstring.toString().getBytes()); 
     System.out.println("i m in end of get vcardstring"); 

    } catch (Exception e1) 
    { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
finally 
{ 
try { 
fis.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
} 

この関数を呼び出した後、uは をクリックした人の連絡先IDはまた、マニフェストファイルに

を2つの権限を追加役立ち、それは完全に私のポストを気に読んで
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission> 

上記のコードはurの問題を解決し、何か問題がある場合は私に再度尋ねます。 問題が解決したら私の答えを投票してください。

+0

おかげでそれは私のために働き、常にスタックにあなたの情報を投稿します。それは私たちを助けます。 – itechDroid

関連する問題