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?
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.