Export the Contacts as VCF file

前端 未结 8 653
梦毁少年i
梦毁少年i 2020-12-01 03:16

I want to export the Phone contacts to External storage area. I didn\'t work with this type of method. Anyone guide me to do this?

相关标签:
8条回答
  • 2020-12-01 04:03

    Android Nougat Update :

    Other answers code working for lots of people before Nougat update.

    Please take care of :

    byte[] buf = new byte[(int) fd.getDeclaredLength()];
    

    is not working on Android Nougat.

    fd.getDeclaredLength() is always return -1.

    Please use below code for read bytes without any library :

    byte[] buf = readBytes(fis);
    
    public byte[] readBytes(InputStream inputStream) throws IOException {
        // this dynamically extends to take the bytes you read
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    
        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
    
        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
    
        // and then we can return your byte array.
        return byteBuffer.toByteArray();
    }
    

    The method readBytes() get from this answer.

    0 讨论(0)
  • 2020-12-01 04:03

    I tried above two codes and I got the .VCF file too, but it was containing only one contact. so here is Perfectly Edited and running code....you will get all contacts in .VCF file:

    private void getVcardString() throws IOException {
        // TODO Auto-generated method stub
        vCard = new ArrayList<String>();  // Its global....
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if(cursor!=null&&cursor.getCount()>0)
        {
            int i;
            String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
            cursor.moveToFirst();
            for(i = 0;i<cursor.getCount();i++)
            {
                get(cursor);
                Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i));
                cursor.moveToNext();
                mFileOutputStream.write(vCard.get(i).toString().getBytes());
            }
            mFileOutputStream.close();
            cursor.close();
        }
        else
        {
            Log.d("TAG", "No Contacts in Your Phone");
        }
    }
    

    Second Method:

    private void get(Cursor cursor2) {
        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");
    
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String vcardstring= new String(buf);
            vCard.add(vcardstring);
        } catch (Exception e1) 
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    

    Please Don't forget to add :

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    0 讨论(0)
提交回复
热议问题