public class ContactHelper { // 신규 추가(성명, 휴대폰번호, 사무실번호, 이메일, 사진, IDX, groupId) public static void insertPhoneContacts(Context context, String display_name, String mobileNO, String officeNO, String strEmail, String strPhoto, String strIDX, long groupId) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ContentProviderOperation.Builder op = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null); ops.add(op.build());
// DISPLAY NAME(성명) op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, display_name); ops.add(op.build());
// 그룹 세팅 op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId); ops.add(op.build());
// 휴대폰 번호 op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNO) .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, strIDX) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); ops.add(op.build());
// 사무실 번호 op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, officeNO) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
op.withYieldAllowed(true); ops.add(op.build());
// EMAIL op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA, strEmail) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK); ops.add(op.build());
// Photo Image Bitmap cachedImage = DownloadImageFromPath(strPhoto); if (cachedImage != null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); cachedImage.compress(CompressFormat.JPEG, 100, out);
byte[] b = out.toByteArray();
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Photo.DATA15, b); ops.add(op.build());
}
try { context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } catch (Exception e) { Log.e("ContactsAdder", "Exceptoin encoutered while inserting contact: " + e); }
}
// 이미지 다운로드 private static Bitmap DownloadImageFromPath(String path) { InputStream instream = null; Bitmap image = null; int responseCode = -1; try {
URL url = new URL(path); // "http://192.168.xx.xx/imagepath/img1.jpg HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // download instream = conn.getInputStream(); image = BitmapFactory.decodeStream(instream); instream.close(); }
} catch (Exception ex) { Log.e("Exception", ex.toString()); } return image; } } |