1、获取手机联系人
获取手机信息前得获取权限
在得到权限后就能获取到手机联系人的信息
在manifest里面注册权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
判断是否有权限
//请求权限
public void requestPermission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED){//应用需求权限是否已经授权0已授权-1没有授权
//有权限的处理事件
// List<Contact> contacts=getContacts();
// for (Contact c:contacts) {
// Log.d("","re===="+c.toString());
// }
} else {//没有权限进行申请
ActivityCompat.requestPermissions(this
,new String[]{Manifest.permission.READ_CONTACTS},1);
}
}
获取联系人数据
//获取联系人信息
private List<Contact> getContacts() {
List<Contact> contacts=new ArrayList<>();
ContentResolver resolver = getContentResolver();
Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI;//电话的URI
Cursor cursor = resolver.query(uri
, new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}//手机的联系人,联系电话号
, null, null, null);
String sql="insert into phonepeople(name,phone) values(?,?)";
SQLiteDatabase db=helper.getWritableDatabase();
while (cursor.moveToNext()){
String name=cursor.getString(0);
String phone=cursor.getString(1);
contacts.add(new Contact(name,phone));
db.execSQL(sql,new Object[]{name,phone});
}
cursor.close();//关闭
return contacts;
}
附加一个请求权限的返回处理
//处理权限返回值
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1//判断请求码是否匹配到
&&Manifest.permission.READ_CONTACTS.equals(permissions[0])//权限是否匹配
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){//返回的数值判断是否获得权限0获得-1没有
// List<Contact> contacts=getContacts();
// for (Contact c:contacts) {
// Log.d("","on===="+c.toString());
// }
}else {
Toast.makeText(this, "您没有权限读取联系人信息", Toast.LENGTH_LONG).show();
}
}
2、获得手机相册的里的图片
private void getPicFromAlbum() {
//跳转到相册去挑选
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");//
startActivityForResult(intent,REQUEST_ALBUME);//需要返回数据
}
处理返回的图片方式,根据业务需求可以进行自己设置,在此只是显示出来
//处理返回数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_ALBUME){
//图库
Uri uri=data.getData();
imageView.setImageURI(uri);//将图库照片显示
}else if (requestCode==REQUEST_IMAGE){
//拍照
imageView.setImageURI(imageuri);//将拍的照片显示
}
}
3、调用手机拍照
在manifest里面设置
<provider
android:authorities="com.hfzy.mydome0805.dome1.fileprovider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true">
<meta-data android:resource="@xml/file_path" android:name="android.support.FILE_PROVIDER_PATHS"/>
</provider>
创建@xml/file_path文件
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path
name="external_cache_path"
path="."/>
</paths>
最后在进行调用
File file=new File(getExternalCacheDir(),"temp.jpg");
imageuri=FileProvider.getUriForFile(this,"com.hfzy.mydome0805.dome1.fileprovider",file);
private void getPicFromCamera() {
//拍照
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);//拍的图片存储在哪里
startActivityForResult(intent,REQUEST_IMAGE);
}
返回值的处理同上面相册返回数据的处理
联系人完整代码文章来源:https://www.toymoban.com/news/detail-529287.html
public class MainActivity extends AppCompatActivity {
private List<Contact> contactList=new ArrayList<>();
private DBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DBHelper(this,2);//创建表格
ListView listView=findViewById(R.id.listV);
requestPermission();
contactList=getContacts();
Myadapt myadapt=new Myadapt(this,contactList);//自定义的适配器
listView.setAdapter(myadapt);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent();
Contact contact=contactList.get(i);
intent.putExtra("name",contact.getName());
intent.putExtra("phone",contact.getPhone());
setResult(RESULT_OK,intent);
finish();
}
});
}
//请求权限
public void requestPermission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED){//应用需求权限是否已经授权0已授权-1没有授权
//有权限的处理事件
// List<Contact> contacts=getContacts();
// for (Contact c:contacts) {
// Log.d("","re===="+c.toString());
// }
} else {//没有权限进行申请
ActivityCompat.requestPermissions(this
,new String[]{Manifest.permission.READ_CONTACTS},1);
}
}
//处理权限返回值
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1//判断请求码是否匹配到
&&Manifest.permission.READ_CONTACTS.equals(permissions[0])//权限是否匹配
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){//返回的数值判断是否获得权限0获得-1没有
// List<Contact> contacts=getContacts();
// for (Contact c:contacts) {
// Log.d("","on===="+c.toString());
// }
}else {
Toast.makeText(this, "您没有权限读取联系人信息", Toast.LENGTH_LONG).show();
}
}
//获取联系人信息
private List<Contact> getContacts() {
List<Contact> contacts=new ArrayList<>();
ContentResolver resolver = getContentResolver();
Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI;//电话的URI
Cursor cursor = resolver.query(uri
, new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}//手机的联系人,联系电话号
, null, null, null);
String sql="insert into phonepeople(name,phone) values(?,?)";
SQLiteDatabase db=helper.getWritableDatabase();
while (cursor.moveToNext()){
String name=cursor.getString(0);
String phone=cursor.getString(1);
contacts.add(new Contact(name,phone));
db.execSQL(sql,new Object[]{name,phone});
}
cursor.close();//关闭
return contacts;
}
}
相册和拍照的完整代码文章来源地址https://www.toymoban.com/news/detail-529287.html
public class AfterActivity extends AppCompatActivity implements View.OnClickListener {
public static final int REQUEST_ALBUME=1;
public static final int REQUEST_IMAGE=2;
private ImageView imageView;
private Uri imageuri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_after);
Button button=findViewById(R.id.But1);
Button button1=findViewById(R.id.But2);
imageView=findViewById(R.id.IV);
button.setOnClickListener(this);
button1.setOnClickListener(this);
// File filepath=getExternalCacheDir();
// imageuri = Uri.fromFile(file);
File file=new File(getExternalCacheDir(),"temp.jpg");
imageuri=FileProvider.getUriForFile(this,"com.hfzy.mydome0805.dome1.fileprovider",file);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.But1:
getPicFromAlbum();
break;
case R.id.But2:
getPicFromCamera();
break;
}
}
private void getPicFromCamera() {
//拍照
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);//拍的图片存储在哪里
startActivityForResult(intent,REQUEST_IMAGE);
}
private void getPicFromAlbum() {
//跳转到相册去挑选
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");//
startActivityForResult(intent,REQUEST_ALBUME);//需要返回数据
}
//处理返回数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_ALBUME){
//图库
Uri uri=data.getData();
imageView.setImageURI(uri);//将图库照片显示
}else if (requestCode==REQUEST_IMAGE){
//拍照
imageView.setImageURI(imageuri);//将拍的照片显示
}
}
}
到了这里,关于Android 获取手机应用数据 联系人,图片,拍照等的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!