1

I want to save the contacts as a .vcf file. I also want to share this .vcf file I created (Gmail, etc.).

The following code works perfectly. I can see the contact list in LogCat. But I couldn't see it in any kind of device storage. I gave the user permission. But it doesn't register. I want to save and share this generated .vcf file. Any help, I'd appreciate it.

My Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Share_VCF_Button = findViewById(R.id.Share_VCF_Button );

        Share_VCF_Button .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            //Share .vcf file with Gmail, etc.
            }
        });

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS},RequestContactPermissionID);
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},RequestWritePermissionID);

        vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";

        getVcardString();
    }
    private void getVcardString() {

        // TODO Auto-generated method stub
        vCard = new ArrayList<String>();
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if(cursor!=null&&cursor.getCount()>0)
        {
            cursor.moveToFirst();
            for(int i =0;i<cursor.getCount();i++){

                get(cursor);
               // Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i));
                cursor.moveToNext();
            }
        }
        else
        {
            Log.d("TAG", "No Contacts in Your Phone");
        }
    }

    public void get(Cursor cursor) {

        //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());
            out.close();

            //I can show vcf logs.
            Log.d("Vcard",  VCard);


        } catch (Exception e1){

            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

My AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Edit;

Everything's working right now. I see the .vcf file I created in storage. But it doesn't write all the contacts to the .vcf file. I see it all on LogCat. But it does not repeat in the .vcf file. Although I can see everyone in my contact list in logcat, why can't I see it in the .vcf file?

İsa C.
  • 329
  • 5
  • 16
  • 1
    See https://stackoverflow.com/q/32789157/115145 and https://stackoverflow.com/q/32635704/115145 and https://stackoverflow.com/q/23353173/115145 – CommonsWare Aug 10 '19 at 15:39
  • :) Thank you for your answer. Everything's working right now. I see the .vcf file I created in storage. But it doesn't write all the contacts to the .vcf file. I see it all on LogCat. But it does not repeat in the .vcf file. @CommonsWare – İsa C. Aug 10 '19 at 15:54

0 Answers0