Skip to content

Instantly share code, notes, and snippets.

@rayworks
Created January 22, 2017 02:57
Show Gist options
  • Save rayworks/8d6acadc15276d895d8ac67cdadeb62a to your computer and use it in GitHub Desktop.
Save rayworks/8d6acadc15276d895d8ac67cdadeb62a to your computer and use it in GitHub Desktop.
private void doImport() {
Uri fullContacts = Uri.parse("content://com.android.contacts/raw_contacts");
Uri itemContact = Uri.parse("content://com.android.contacts/data");
int importCnt = 0;
try {
InputStream inputStream = getAssets().open("ct2.txt");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int readCnt;
while ((readCnt = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, readCnt);
}
bos.flush();
String rawStr = bos.toString();
bos.close();
int beg = 0;
int end;
int nameBeg = 0;
while ((nameBeg = rawStr.indexOf("name=", beg)) >= 0) {
beg = nameBeg + "name=".length();
end = rawStr.indexOf(",", beg);
String name = rawStr.substring(beg, end);
int phoneBeg = rawStr.indexOf("phone=", end) + "phone=".length();
int phoneEnd = rawStr.indexOf("\n", phoneBeg);
if (phoneEnd < 0) { // buggy for retrieving the last record due to "contractID" field!!!
break;
}
String phone = rawStr.substring(phoneBeg, phoneEnd);
++importCnt;
Uri uri = fullContacts; //Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = getApplication().getContentResolver();
// for each contact :
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
//ContentValues contentValues = new ContentValues();
//long id = ContentUris.parseId(resolver.insert(uri, contentValues));
ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
.withValue("account_name", null)
.build();
operations.add(op1);
uri = itemContact; //Uri.parse("content://com.android.contacts/data");
ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/name")
.withValue("data2", name)
.build();
operations.add(op2);
ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
.withValue("data1", phone)
.build();
operations.add(op3);
try {
resolver.applyBatch("com.android.contacts", operations);
//Toast.makeText(this, ":) 成功添加联系人!", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
//Toast.makeText(this, ":( 添加一位联系人失败!", Toast.LENGTH_LONG).show();
} catch (OperationApplicationException e) {
e.printStackTrace();
//Toast.makeText(this, ":( 添加一位联系人失败!", Toast.LENGTH_LONG).show();
}
beg = phoneEnd;
System.out.println("Name:" + name + " | " + "phone:" + phone);
}
Toast.makeText(this, String.format("成功导入 %d 位联系人!", importCnt), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void doExport() {
//this.getApplicationContext().getExternalFilesDir()
//Environment.getExternalStorageDirectory();
File file = new File(/*getExternalFilesDir(null)*/ Environment.getExternalStorageDirectory(), "contact.txt");
System.out.println(TAG + ": dest file | " + file.getAbsolutePath());
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
Uri uri = Uri.parse("content://com.android.contacts/contacts");
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
while (cursor.moveToNext()) {
int contractID = cursor.getInt(0);
StringBuilder sb = new StringBuilder("contractID=");
sb.append(contractID);
uri = Uri.parse("content://com.android.contacts/contacts/" + contractID + "/data");
Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);
while (cursor1.moveToNext()) {
String data1 = cursor1.getString(cursor1.getColumnIndex("data1"));
String mimeType = cursor1.getString(cursor1.getColumnIndex("mimetype"));
if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名
sb.append(",name=" + data1);
} else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { //邮箱
sb.append(",email=" + data1);
} else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手机
sb.append(",phone=" + data1).append("\n");
}
}
cursor1.close();
// copy
byte[] bts = sb.toString().getBytes();
fos.write(bts);
Log.i(TAG, sb.toString());
}
cursor.close();
fos.flush();
Toast.makeText(this, ":) 导出联系人成功", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
/** ignore **/
}
}
Toast.makeText(this, ":( 未能成功导出联系人", Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment