Export the Contacts as VCF file

前端 未结 8 706
梦毁少年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.

提交回复
热议问题