我需要联系人的照片。所以我得到了联系人的身份证明以及获得照片的下列功能。
Log.i(MenuActivity.TAG, "START: getContactPhoto; PARAMETERS: id: " + String.valueOf(id));
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
Bitmap photo;
ContentResolver cr = context.getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);
photo = BitmapFactory.decodeStream(is);
return photo;
这是将照片设置为imageView
if (photo != null) {
Log.d(MenuActivity.TAG, "Photo exists");
imageView.setImageBitmap(photo);
} else {
Log.e(MenuActivity.TAG, "No photo for contact " + name);
imageView.setImageResource(R.drawable.defult_foto);
}
我的logcat:
START: getContactPhoto; PARAMETERS: id: 148
Photo uri: content://com.android.contacts/contacts/148
No photo for contact Ivan
我获取联系ID的方式:
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(uri, projection, where, selectionArgs, sortOrder);
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
try {
id = "no-id";
birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// some code
} catch (Exception e) {
Log.e("Days", "No birthday date!");
}
}
}
cursor.close();
但是,我发现我的联系人都没有照片,但它有。我相信smth是错的。有什么想法吗?
P.S我在andrid中有点菜鸟,所以如果不是很难具体
答案 0 :(得分:0)
使用此示例代码获取缩略图
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
android:layout_marginLeft="58dp"
android:layout_marginTop="60dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
活动代码
package com.example.contactsdisplaypic;
import java.io.ByteArrayInputStream;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Context mContext;
public SimpleCursorAdapter sca;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
new LoadContactsTask().execute();
}
class LoadContactsTask extends AsyncTask<Void, Void, Cursor> {
@Override
protected Cursor doInBackground(Void... params) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(uri, projection, where, selectionArgs, sortOrder);
cursor.moveToFirst();
return cursor;
}
@Override
protected void onPostExecute(Cursor result) {
// create an array to specify which fields we want to display
String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1, android.R.id.text2};
// create simple cursor adapter
sca =
new SimpleCursorAdapter(mContext, android.R.layout.simple_expandable_list_item_2, result, from, to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
sca.setDropDownViewResource( android.R.layout.simple_expandable_list_item_2 );
// get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.spinner1 );
s.setAdapter(sca);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
Toast.makeText(mContext, "contact id " + sca.getCursor().getString(2), Toast.LENGTH_LONG).show();
new LoadPicturesTask().execute(sca.getCursor().getString(2));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
}
}
private class LoadPicturesTask extends AsyncTask<String, Void, Bitmap[]> {
@Override
protected Bitmap[] doInBackground(String... contactId) {
Bitmap[] array = new Bitmap[2];
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
Long.parseLong(contactId[0]));
// thumbnail
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {Contacts.Photo.PHOTO}, null, null, null);
try {
if (cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
array[0] = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
}
}
} finally {
cursor.close();
}
return array;
}
@Override
protected void onPostExecute(Bitmap[] result) {
if (result != null) {
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
img1.setImageBitmap(result[0]);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我能够获取存储了生日的一些联系人,然后获取了可用的缩略图。