0

Here is my code to export contacts to VCF file on SDcard. But i can't find vcf on SDCard. Help me find that. Thanks in advance

Code to get VCF file:

public static void getVCF() 

{

 final String vfile = "POContactsRestore.vcf";

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

 phones.moveToFirst();
   for(int i =0;i<phones.getCount();i++)
   {
      String lookupKey =  phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
     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);
         String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
         FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                    mFileOutputStream.write(VCard.toString().getBytes());           
         phones.moveToNext();                           
         Log.d("Vcard",  VCard);
     } 
     catch (Exception e1) 
     {
          // TODO Auto-generated catch block
          e1.printStackTrace();
     }

 }
}

Manifest // permisson

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Tuan Vu
  • 6,397
  • 2
  • 14
  • 22

1 Answers1

0

try:

    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + file.separator + vfile;
David M
  • 2,511
  • 1
  • 15
  • 18
  • can you verify (via debug) that the "path" is pointing where you want it?? also, where do you call `mFileOutputStream.close()`? – David M Nov 02 '12 at 16:32
  • i'm beggin in android, could you show me how to verify "path" via logcat or debug? – Tuan Vu Nov 02 '12 at 16:36
  • i assume you are using Eclipse. if so you can right-click on the beginning of a line and "set breakpoint". to start the app in DEBUG mode, simply hit F11. the debugger should stop at the breakpoint. – David M Nov 02 '12 at 16:38
  • pls "up vote" my answer if it helped you. good luck on your future projects! – David M Nov 02 '12 at 17:35
  • break the function down into the 2 separate things you are trying to accomplish (READing a file and WRITEing a file). debug them separately, when they both work, recombine them and viola! it'll work. – David M Nov 05 '12 at 11:15
  • Thanks @David M, i found the vcf file on real device – Tuan Vu Nov 15 '12 at 01:32