0

I am currently passing contact name and number with an intent. Which works fine. I am however unable to find out how I can pass a contacts photo and assign it to an ImageView in activity.

I've searched around here and not found an easy answer to this at all.

In case it's neccessary, my method to get name and number of whomever is calling:

private String getContactName(String number) {

    String name = null;

    String[] projection = new String[]{
            ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup._ID};


    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    Cursor cursor = this.c.getContentResolver().query(contactUri, projection, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        }
        cursor.close();
    }

    return name;
}

It gets passed to my activity:

Bundle bundle = new Bundle();
            bundle.putString("Number", number);
            bundle.putString("Name", this.getContactName(number));
            intent.putExtras(bundle);

Received with

Bundle bundle = this.getIntent().getExtras();
        if (bundle.getString("Name").isEmpty()) {
            this.name.setText("Unknown Caller");
        } else {
            this.name.setText(bundle.getString("Name"));
        }

        if (bundle.getString("Number").isEmpty()) {
            this.number.setText(bundle.getString("Number"));
        } else {
            this.number.setText(bundle.getString("Name"));
        }
Carl
  • 249
  • 2
  • 5
  • 13

2 Answers2

0

You can take contactID from this cursor or your code. This is my method which will give you contact photo, just pass contact ID in it:

 public InputStream openPhoto(long contactId) {
     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
     Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
     Cursor cursor = getContentResolver().query(photoUri,
          new String[] {Contacts.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;
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

try this you will get byte[] of contact photo pass it to intent and decode in second activity

 private byte[] queryContactImage(int imageDataRow) {
    Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {
        ContactsContract.CommonDataKinds.Photo.PHOTO
    }, ContactsContract.Data._ID + "=?", new String[] {
        Integer.toString(imageDataRow)
    }, null);
    byte[] imageBytes = null;
    if (c != null) {
        if (c.moveToFirst()) {
            imageBytes = c.getBlob(0);
        }
        c.close();
    }
    return imageBytes;
}

and in second activity

if (imagebyte != null) {
        Bitmap bitimg2 = BitmapFactory.decodeByteArray(imagebyte, 0, imagebyte.length); 
        yourimageview.setImageBitmap(bitimg2);
    }

hope this will help you