Finally, I solved it.
The problem was that I tried to retrieve direct the contact's image. I
had to request for contacts at first and then request for the
contact's image.
Here are the piece of code I implemented. It worked properly:
[code]
//Expression of intent
Intent i = new Intent (Intent.ACTION_GET_CONTENT);
i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
[/code]
and then in the onActivityResult()
[code]
Uri uri = data.getData();
if (uri != null)
{
Cursor c = null;
try
{
//Retrieve contacts
c = getContentResolver().query(uri, new
String[] { BaseColumns._ID },
null, null, null);
if (c != null && c.moveToFirst())
{
int id = c.getInt(0);
//Now get contact's photo
//..
//int id =
c.getColumnIndex(BaseColumns._ID);
InputStream is = openPhoto(id);
if (is != null)
{
Bitmap photo =
BitmapFactory.decodeStream(is);
imageview.setImageBitmap(photo);
}
else
{
Toast.makeText(PickupContactInfo.this, "No photo
available!", Toast.LENGTH_LONG).show();
}
is.close();
}
}
catch(Exception e)
{
if (c != null)
{
c.close();
}
}
}
[/code]
where openPhoto()
[code]
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[]
{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;
}
[/code]
Regards,
kalgik
On May 14, 11:57 am, kalgik <[email protected]> wrote:
> Hello everybody.
> I would like to pick up a photo of the contact and display it in my
> application in ImageView().
>
> The problem seems to be in the description of that Intent.
> I am writing the following:
> [code]
> Intent i = new Intent (Intent.ACTION_GET_CONTENT);
> i.addCategory(Intent.CATEGORY_OPENABLE);
> // 1 -
> i.setType(ContactsContract.CommonDataKinds.Photo.PHOTO); //- > Force
> close
> // 2 - i.setType("*/*");
> startActivityForResult(i,PICK_IMAGE_FROM_CONTACT);
> [/code]
>
> If I set the type of intent as
> "ContactsContract.CommonDataKinds.Photo.PHOTO" the application force
> closes.
> If I set the type of intent as "*/*" the application prompt me to pick
> a picture from Gallery and some other application but not from the
> Contacts.
>
> How do I have to express this Intent fro picking a photo from a
> specific contact?
>
> I have successfully retrieved information from the contacts(except
> photo) and pick a photo from Gallery.
> I appreciate any suggestions.
>
> Regards,
> kalgik
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en