一:可以先看看这个程序的演示(运作)流程:
1.点击第一个按钮,选择系统联系人
2.到系统的通讯录去任选一个联系人(前提是:自己要手动在系统模拟器的通讯录中手动添加几个哦)
3.效果
4.点击第二个按钮,获取系统相册的图片(与“1”同理,我就不放图片了,哈哈)
5.到系统相册选择任意一张图片
6.总效果
话好像说多了,有点啰嗦呢 (嘿嘿)
二:代码
1.要在AndroidManifest.xml里添加权限
<!--获取系统手机联系人权限-->
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--获取系统相册权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.布局文件(由于每个人的模拟器大小参数配置不一样,展现出来的效果可能会有一点点的小差异,没关系,自己调整修改一下就可以啦)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/sm"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_contact"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:background="#FB8C00"
android:text="获取系统联系人信息"
/>
<Button
android:id="@+id/btn_picture"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginLeft="230dp"
android:layout_marginTop="100dp"
android:background="#FB8C00"
android:text="获取系统相册信息"
/>
</RelativeLayout>
<TextView
android:id="@+id/nameMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="25sp"/>
<TextView
android:id="@+id/phoneMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="25sp"/>
<ImageView
android:id="@+id/img_photos"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:padding="20dp"/>
</LinearLayout>
3.Java代码
public class MainActivity extends AppCompatActivity {
private static final int PICK_CONTACT = 1;//很多人会以这种方式将设置请求码,防止出现多个请求对象而记混哦
private Button btn_pictures;
private ImageView img_photos;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取系统联系人点击事件
Button btn_contact = findViewById(R.id.btn_contact);
btn_contact.setOnClickListener(v -> {//这里用的是Lambda表达式
//运行时获取读取联系人信息的权限
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
1);
});
btn_pictures = findViewById(R.id.btn_picture);
img_photos = findViewById(R.id.img_photos);
//获取系统图片点击事件
btn_pictures.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(intent ,2);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
//如果用户同意授权访问
if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CONTACT) {
if (resultCode == Activity.RESULT_OK) {
//获取返回的数据
Uri contactData = data.getData();
CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);
//查询联系人信息
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null && cursor.moveToFirst()) {
String contactId = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人名字
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = "此联系人暂未存入号码";
//根据联系人查询此联系人的详细信息
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
if (phones != null && phones.moveToFirst()) {
//取出电话号码
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
//关闭游标
if (phones != null) {
phones.close();
}
//显示联系人的详细信息
TextView nameMsg = findViewById(R.id.nameMsg);
nameMsg.setText("您的用户名为:" + name);
TextView phoneMsg = findViewById(R.id.phoneMsg);
phoneMsg.setText("您的电话号码为:" + phoneNumber);
}
}
}
if (requestCode==2){
if (data !=null){
Uri uri=data.getData();
img_photos.setImageURI(uri);
}
}
}
}
好了,大功告成
代码亲测有效,欢迎大家参考((#^.^#))
鄙人第一次写博客,请大家多多指教