0

I have successfully created .vcf file for the all contacts from my android device. I have referred below link for doing so :

Export the Contacts as VCF file

and its working quite good. But, I need to convert a single contact to single .vcf file as per my requirement.

In this current code getting all the contacts using below lines :

cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

and then all contacts will be covered in .vcf file.

So, What is way that I can generate .vcf file for a single contact ?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • pass a new file name everytime in the get() method code from your link – Vivek Mishra Sep 19 '18 at 04:40
  • Will you pls. explain ? – Jaimin Modi Sep 19 '18 at 04:42
  • Did you understood how the get() method works in that link ? – Vivek Mishra Sep 19 '18 at 04:44
  • It's getting column index from lookup key and then generating uri from that lookupKey then that Uri is used to initialize AssetFileDescriptor. Now, this object of AssetFileDescriptor is used to initialize FileInputStream and bytearray. Bytearray is passing as an argument for read method. Finally, creating string instance which is going to be add inside vCard ArrayList which is going to be saved using FileOutPutStream. – Jaimin Modi Sep 19 '18 at 04:51
  • It is not saving vcard arraylist actually. It is just keeping an arraylist separately but only writing the current string in the file. – Vivek Mishra Sep 19 '18 at 04:58
  • And it is writing it in the same file everytime. So instead of writing that in same file, you can create a new file everytime. – Vivek Mishra Sep 19 '18 at 04:59
  • okay sir, Let me try out. – Jaimin Modi Sep 19 '18 at 05:03
  • 1
    Please check my answer below.That's what I am saying you to try out. – Vivek Mishra Sep 19 '18 at 05:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180316/discussion-between-jaimin-modi-and-vivek-mishra). – Jaimin Modi Sep 19 '18 at 06:20

1 Answers1

1

See the first line in the below code block. Here I am creating a new vFile everytime, this method will be called.So each contact will be saved in different files.

public void get(Cursor cursor)
    {
        vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";


        //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 = this.getContentResolver().openAssetFileDescriptor(uri, "r");

            // Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??


           /* 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);*/

            FileInputStream 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;
            FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
            mFileOutputStream.write(vcardstring.toString().getBytes());

        } catch (Exception e1) 
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • I have just added a int flag like : String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile+""+flag; flag=flag+1; – Jaimin Modi Sep 19 '18 at 05:08
  • But, Sir Will you pls. suggest me that which field can I take if I want user Inputs for Contact Fields and then on pressing Button click that given inputs will be generated to .vcf. Because in this current way, If in User's device if there 1000 contacts then 1000 .vcf file will be created. Or, Is there a way so that I can select particular contact and then it will going to converted to .vcf ? Thanks. – Jaimin Modi Sep 19 '18 at 05:10
  • For that purpose first show the contacts list with checkboxes to the user to select which contacts to convert in vcf. Then only convert the selected contacts – Vivek Mishra Sep 19 '18 at 05:13
  • And sir, Another issue is Its done its task but there is no user confirmation. what should I do for that ? How can i implement AsyncTask here or something else ? – Jaimin Modi Sep 19 '18 at 05:14
  • 1
    simply use the above method code in doInBackground of asynctask – Vivek Mishra Sep 19 '18 at 05:15