我有一个制造按钮。单击它将打开我们的电话簿,我们可以选择一个联系人,它将显示在我们的RecyclerView
应用程序中。我正在提取联系人ID,姓名,照片,电话号码。
正常工作 - 当我检索在电话簿中有照片的联系人时
出现错误 - 当我在没有照片的情况下检索联系人时,应用程序因msg应用程序停止工作而崩溃
提前帮助我guyn,thnx
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ImportantContact extends AppCompatActivity {
private static final String TAG = ImportantContact.class.getSimpleName();
private static final int REQUEST_CODE_PICK_CONTACTS = 1;
private Uri uriContact;
private String contactID; // contacts unique ID
Bitmap photo;
String contactNumber;
String contactName;
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
List<ImportantPeopleDataset> list = new ArrayList<>();
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.important_contact);
toolbar = (Toolbar) findViewById(R.id.imp_contacts_toolbar);
setSupportActionBar(toolbar);
//list.add(new ImportantPeopleDataset("Dinesh", "8889731492", R.drawable.bharat));
mRecyclerView = (RecyclerView) findViewById(R.id.nav_imp_contacts_recyclerview);
mRecyclerView.setHasFixedSize(true);
//mLayoutManager=new LinearLayoutManager(this);
mLayoutManager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyImportantContactAdapter(list);
mRecyclerView.setAdapter(mAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
onClickSelectContact(view); //App crashes when a contact without pic is selected
}
});
}
//-----------------------------------------------------------------------------------------------------------
public void onClickSelectContact(View btnSelectContact) {
// using native contacts selection
// Intent.ACTION_PICK = Pick an item from the data, returning what was selected.
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactName();
retrieveContactNumber();
retrieveContactPhoto();
}
}
private void retrieveContactPhoto() {
photo = BitmapFactory.decodeResource(this.getResources(),
R.drawable.bharat);
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
//ImageView imageView = (ImageView) findViewById(R.id.img_contact);
//imageView.setImageBitmap(photo);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
list.add(new ImportantPeopleDataset(contactName, contactNumber, photo));
mAdapter.notifyDataSetChanged();//notify item remove b hota h remove krte tume use kr lena
}
private void retrieveContactNumber() {
contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).trim().replace(" ", "");
} //.trim().replace(" ","") is used for removing extra space in phone number
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
private void retrieveContactName() {
contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
}
}
important_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/imp_contacts_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:padding="5dp"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_imp_contacts_recyclerview"
>
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add" />
</android.support.design.widget.CoordinatorLayout>
MyImportantContactAdapter.java
package com.bharat.fcr;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by Dinesh on 3/25/2016.
*/
public class MyImportantContactAdapter extends android.support.v7.widget.RecyclerView.Adapter<MyImportantContactAdapter.ViewHolder> {
List<ImportantPeopleDataset> list = Collections.emptyList();
public MyImportantContactAdapter(List<ImportantPeopleDataset> list) {
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_important_contacts, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.number.setText(list.get(position).getNumber());
holder.name.setText(list.get(position).getName());
holder.pic.setImageBitmap(list.get(position).getBmp());
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, number;
CircleImageView pic;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.cardview_imp_contacts_name);
number = (TextView) itemView.findViewById(R.id.cardview_imp_contacts_number);
pic = (CircleImageView) itemView.findViewById(R.id.cardview_imp_contacts_profile);
}
}
}
cardview_important_contacts.xml - 没有使用cardview忽略名称格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:id="@+id/cardview_imp_contacts_profile"
android:layout_height="80dp"
android:src="@drawable/bharat"/>
<TextView
android:textColor="#4b3e3e"
android:id="@+id/cardview_imp_contacts_name"
android:paddingLeft="4dp"
android:paddingTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Name"
/>
<TextView
android:textColor="#4b3e3e"
android:id="@+id/cardview_imp_contacts_number"
android:paddingLeft="4dp"
android:paddingTop="2dp"
android:paddingBottom="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Pnone Number"
/>
</LinearLayout>
</LinearLayout>