Reputation: 696
I want a function which takes in a contact id(long) and returns the corresponding contact pic(Bitmap or InputStream) Have tried a lot. But I am not being able to pull it off.
PS - Min API Level = 10
Upvotes: 0
Views: 1002
Reputation: 18592
Try the following code:
private void setContactInfo(long id){
Bitmap photoBitmap = null;
Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id);
Cursor cursor = managedQuery(contactUri, null, null, null, null);
cursor.moveToFirst();
contact_text.setText(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));//contact.text is a textView used to displays the contact name
String id = getIntent().getData().getLastPathSegment();
// Photo cursor
String photoWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] photoWhereParams = new String[] { id,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };
Cursor photoCur = managedQuery(ContactsContract.Data.CONTENT_URI, null,
photoWhere, photoWhereParams, null);
photoCur.moveToFirst();
if (photoCur.moveToFirst() && photoCur != null) {
byte[] photoBlob = photoCur.getBlob(photoCur
.getColumnIndex(Photo.PHOTO));
if (photoBlob != null) {
photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0,
photoBlob.length);
contact_image.setImageBitmap(photoBitmap);//contact_image is an ImageView
} else {
photoBitmap = BitmapFactory.decodeResource(getResources(),
android.R.drawable.ic_menu_report_image);//android.R.drawable.ic_menu_report_image is the default image if a Contact doesn't have any image stored
contact_image.setImageBitmap(photoBitmap);
}
}
cursor.close;
photoCur.close;
}
Hope this helps.
Upvotes: 3
Reputation: 696
private ByteArrayInputStream getPhoto()
{
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
if (cursor == null) {
return null;
}
try
{
if (cursor.moveToFirst())
{
byte[] data = cursor.getBlob(0);
if (data != null)
{
return new ByteArrayInputStream(data);
}
}
}
finally
{
cursor.close();
}
return null;
}
This is my code. It is working.
Upvotes: 0
Reputation: 1791
In your activity you need the url to download the picture. Use the following methods(make sure this code must be in your that activity, where you want to download the picture):
private class DownloadProfilePicture extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
InputStream in = null;
int response = -1;
URL url = "your image url";
URLConnection conn = null;
HttpURLConnection httpConn = null;
conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
if (in != null && in.available() > 807) {
yourBitmaptype.setBitmap(
BitmapFactory.decodeStream(in));
} else {
users.get(screenName).setBitmap(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
}
in.close();
in = null;
} catch (Exception e) {
users.get(temp).setBitmap(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
Log.e(TAG, "Downloading Image Exception.. Using default");
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// use post execute logic
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// use pre execute logic
super.onPreExecute();
}
}
and call it from onCreate()
as new DownloadProfilePicture().execute();
Upvotes: 0