forked from angcyo/ContactsPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 71e6f53
Showing
26 changed files
with
921 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 25 | ||
buildToolsVersion "25.0.2" | ||
defaultConfig { | ||
applicationId "com.angcyo.contactspicker" | ||
minSdkVersion 14 | ||
targetSdkVersion 25 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', include: ['*.jar']) | ||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
}) | ||
compile 'com.android.support:appcompat-v7:25.1.0' | ||
compile 'com.android.support:design:25.1.0' | ||
testCompile 'junit:junit:4.12' | ||
|
||
compile 'io.reactivex:rxjava:1.2.5' | ||
compile 'io.reactivex:rxandroid:1.2.1' | ||
|
||
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.1' | ||
|
||
compile 'com.github.promeg:tinypinyin:2.0.0' // ~80KB | ||
|
||
compile 'com.github.bumptech.glide:glide:3.7.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /Users/angcyo/Library/Android/sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.angcyo.contactspicker"> | ||
|
||
<!--读取联系人权限--> | ||
<uses-permission android:name="android.permission.READ_CONTACTS"/> | ||
<uses-permission android:name="android.permission.WRITE_CONTACTS"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme.NoActionBar"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
|
||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
150 changes: 150 additions & 0 deletions
150
app/src/main/java/com/angcyo/contactspicker/ContactsPickerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package com.angcyo.contactspicker; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.provider.ContactsContract; | ||
import android.text.TextUtils; | ||
|
||
import com.github.promeg.pinyinhelper.Pinyin; | ||
|
||
/** | ||
* Created by angcyo on 2017-01-08. | ||
*/ | ||
|
||
public class ContactsPickerHelper { | ||
public static void getContactsList(Context context) { | ||
final ContentResolver contentResolver = context.getContentResolver(); | ||
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[]{"_id"}, null, null, null); | ||
L.w("联系人总数量:" + cursor.getCount()); //就是联系人的总数 | ||
|
||
//枚举所有联系人的id | ||
if (cursor.getCount() > 0) { | ||
int count = 0; | ||
if (cursor.moveToFirst()) { | ||
do { | ||
int contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);//获取 id 所在列的索引 | ||
String contactId = cursor.getString(contactIdIndex);//联系人id | ||
|
||
L.e("-------------------------" + count + "----------------------"); | ||
L.w("联系人ID:" + contactId); | ||
final String name = getData1(contentResolver, contactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); | ||
L.w("联系人名称:" + Pinyin.toPinyin(name.charAt(0)).toUpperCase().charAt(0) + " " + name); | ||
L.w("联系人电话:" + getData1(contentResolver, contactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)); | ||
// logData(contentResolver, contactId); | ||
count++; | ||
} while (cursor.moveToNext()); | ||
} | ||
} | ||
|
||
cursor.close(); | ||
} | ||
|
||
/** | ||
* 获取联系人的图片 | ||
*/ | ||
public static Bitmap getPhoto(final ContentResolver contentResolver, String contactId) { | ||
Bitmap photo = null; | ||
Cursor dataCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, | ||
new String[]{"data15"}, | ||
ContactsContract.Data.CONTACT_ID + "=?" + " AND " | ||
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", | ||
new String[]{String.valueOf(contactId)}, null); | ||
if (dataCursor != null) { | ||
if (dataCursor.getCount() > 0) { | ||
dataCursor.moveToFirst(); | ||
byte[] bytes = dataCursor.getBlob(dataCursor.getColumnIndex("data15")); | ||
if (bytes != null) { | ||
photo = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); | ||
} | ||
} | ||
dataCursor.close(); | ||
} | ||
return photo; | ||
} | ||
|
||
/** | ||
* 根据MIMETYPE类型, 返回对应联系人的data1字段的数据 | ||
*/ | ||
private static String getData1(final ContentResolver contentResolver, String contactId, final String mimeType) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
Cursor dataCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, | ||
new String[]{ContactsContract.Data.DATA1}, | ||
ContactsContract.Data.CONTACT_ID + "=?" + " AND " | ||
+ ContactsContract.Data.MIMETYPE + "='" + mimeType + "'", | ||
new String[]{String.valueOf(contactId)}, null); | ||
if (dataCursor != null) { | ||
if (dataCursor.getCount() > 0) { | ||
if (dataCursor.moveToFirst()) { | ||
do { | ||
final String data = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DATA1)); | ||
if (TextUtils.isEmpty(data)) { | ||
continue; | ||
//stringBuilder.append("空"); | ||
} else { | ||
stringBuilder.append(data); | ||
} | ||
stringBuilder.append("_");//多个值,之间的分隔符.可以自定义; | ||
} while (dataCursor.moveToNext()); | ||
} | ||
} | ||
dataCursor.close(); | ||
} | ||
|
||
return stringBuilder.subSequence(0, Math.max(0, stringBuilder.length() - 1)).toString(); | ||
} | ||
|
||
/** | ||
* 打印联系人的所有字段信息 | ||
*/ | ||
private static void logData(final ContentResolver contentResolver, String contactId) { | ||
Cursor dataCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, | ||
null, | ||
ContactsContract.Data.CONTACT_ID + "=?", | ||
new String[]{String.valueOf(contactId)}, null); | ||
log(dataCursor); | ||
} | ||
|
||
private static void log(final Cursor cursor) { | ||
if (cursor != null) { | ||
if (cursor.getCount() > 0) { | ||
L.e("----------------------start--------------------"); | ||
L.i("数量:" + cursor.getCount() + " 列数:" + cursor.getColumnCount()); | ||
if (cursor.moveToFirst()) { | ||
do { | ||
for (int i = 0; i < cursor.getColumnCount(); i++) { | ||
final String columnName = cursor.getColumnName(i); | ||
final int columnIndex = cursor.getColumnIndex(columnName); | ||
final int type = cursor.getType(columnIndex); | ||
String data = "", ty = ""; | ||
if (type == Cursor.FIELD_TYPE_NULL) { | ||
ty = "NULL"; | ||
data = "空值"; | ||
} else if (type == Cursor.FIELD_TYPE_BLOB) { | ||
ty = "BLOB"; | ||
final byte[] bytes = cursor.getBlob(columnIndex); | ||
data = String.valueOf(bytes) + " " + bytes.length * 1f / 1024f + "KB"; | ||
} else if (type == Cursor.FIELD_TYPE_FLOAT) { | ||
ty = "FLOAT"; | ||
data = String.valueOf(cursor.getFloat(columnIndex)); | ||
} else if (type == Cursor.FIELD_TYPE_INTEGER) { | ||
ty = "INTEGER"; | ||
data = String.valueOf(cursor.getInt(columnIndex)); | ||
} else if (type == Cursor.FIELD_TYPE_STRING) { | ||
ty = "STRING"; | ||
data = cursor.getString(columnIndex); | ||
} | ||
|
||
L.i("第" + i + "列->名称:" + columnName + " 索引:" + columnIndex + " 类型:" + ty + " 值:" + data); | ||
} | ||
} while (cursor.moveToNext()); | ||
} | ||
L.e("------------------------end---------------------"); | ||
} | ||
cursor.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.