File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes How to change contacts thumbnail using ContentResolver? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to change contacts thumbnail using ContentResolver?" Watch "How to change contacts thumbnail using ContentResolver?" New topic
Author

How to change contacts thumbnail using ContentResolver?

Alon Minski
Greenhorn

Joined: Apr 26, 2013
Posts: 4
I've been trying for days now to crack this... I simply cant understand how to change or update a contact's thumbnail image.

I am working on an app that has a GridView with all contacts in the device. Each cell, has a ImageView + TextView holding the thumbnail and contact's name. I know how to query the database and get the info I need and then use it to populate the GridView with the contacts name and thumbnail, but for the life of me I cant get the code to update the photo when the user chooses one from the device's gallery.

My situation right now is that I created an Intent for the startActivityForResult() method asking for the content of the selected photo from the device's gallery. Then in the onActivityResult() method, I pull the image using the contentResolver. Now I have a Bitmap with the selected photo from the gallery:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode = REQ_CODE_PICK_IMAGE_FOR_CONTACT && resultCode = RESULT_OK){

Bitmap yourSelectedImage = pullPhotoFromGallery(imageReturnedIntent);
ByteArrayOutputStream stream = new ByteArrayOutputStream();

if (yourSelectedImage != null) {
Bitmap thumbnail = Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, false);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] photo = stream.toByteArray();

//TODO update ContentProvider at the right place....


}


private Bitmap pullPhotoFromGallery(Intent imageReturnedIntent) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
return yourSelectedImage;
}
Can anyone PPPPLEASE help me with the code to update the right column/table in the ContentProvider to get the thumbnail to change?

I'm really lost here...

Thank you!!!


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Have a look at this page for the detials for the Contacts ContentProvider. Somewhere around the first third of the page is some details about the photo. You might be able to use it with an update call (see here) to update the photo.

Also, all the way at the bottom of the contacts provider page there is a link to ContactsContract.RawContacts.DisplayPhoto which has sample code.


Steve
Alon Minski
Greenhorn

Joined: Apr 26, 2013
Posts: 4
Hey Steve ,
I did go over these pages before, I'll give it another try
I guess the answer is buried somewhere in there.

If there are any specific code examples to help me, that also would be great!

Thank you

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Like I said, one of the links at the bottom of the contacts provider page has a code example.
Alon Minski
Greenhorn

Joined: Apr 26, 2013
Posts: 4
OH! How did I miss that??
Thanks!!

Alon Minski
Greenhorn

Joined: Apr 26, 2013
Posts: 4
After reading everything again on that page, it led me to this method located here:

public void writeDisplayPhoto(long rawContactId, byte[] photo) {
Uri rawContactPhotoUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
try {
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
OutputStream os = fd.createOutputStream();
os.write(photo);
os.close();
fd.close();
} catch (IOException e) {
// Handle error cases.
}
}

I added that to my code and used the right parameters, but I still cant get it to work. When I pick the photo from the gallery, I refresh my GridView and the image changes. But when I start the application again, it loads the old original photo... so I assume the DB never got updated...
Also the line RawContacts.DisplayPhoto.CONTENT_DIRECTORY requires API 14 and I use API 10. Is there a different string I can use instead of that so it works with API 10?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change contacts thumbnail using ContentResolver?