Extract a contact's photo - android

Gah, another scenario here something that should be simple is proving to be very time-consuming and painful.
I'm using this to query the contacts provider:
private Cursor getContacts(){
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
......
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
This works fine and retrieves contact names, and on a handful of contacts it shows a numeric ID for the PHOTO_ID field, which I assume is the PHOTO_ID I'm requesting. But then I push that ID into this method to extract the bitmap, it fails on every contact and the stream is null every time. I'm testing against a set of contacts that includes some with Android contact photos (I know there are some issues extracting photos from Facebook contacts).
private Bitmap loadContactPhoto(long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
if (input == null) return null;
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
}
What have I missed?

openContactPhotoInputStream() takes the uri of the contact, try calling it with the ContactsContract.Contacts._ID column instead of the PHOTO_ID column and you should see better results.
There's a bunch of relevant discussion here with some code to check out:
How do I load a contact Photo?
Note that in some cases you'll see a photo in the native contacts app which won't load through the content resolver. Some sync info, like Facebook for example, is flagged to be used only by the contacts app itself and doesn't get exported to other apps :-(
However, using the contactUri should take care of at least some of your issues.

Related

How can I access ContactsContract Profile?

I tried to read the complete profile information such a (Full name, phone, adresse, mail .... ).
I have searched everywhere for a good example code. I tried many ways (Uri => Cursor) to access the Profile.
At this time I can fetch just the Full name (of the Profile contact), nothing more.
I can fetch data of other contacts using an Intent, sending to the Contacts app, BUT I CAN'T READ THE PROFILE CONTACT (JUST FULL NAME).
I have added the READ_PROFILE permission in the manifest file.
With the following code I get the Full Name (I can also access first and last name separately ):
Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor cursorProfile = this.getContentResolver().query(uriProfile,
null, null, null, null);
String projection = Profile.DISPLAY_NAME;
String profileName = cursorProfile.getString(cursorProfile.getColumnIndex(projection);
But when I use this the following projection to get Phone Number, it returns an error and the app stops working:
String projection = ContactsContract.CommonDataKinds.Phone.NUMBER
I found a solution using this code:
Get an URI Profile
Crate a cursor pointing to the URI content with null projection. Because for the profile, data are saved differently than a normal contact.
Point the cursor to the wanted data using MIMETYPE.
Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor cursorProfile = getApplicationContext().getContentResolver().query(uriProfile,
null, null, null, null);
String cursorProfile_MIMIETYPE = cursorProfile.getString(cursorProfile.getColumnIndex("MIMETYPE"));

How to get last image captured with camera in android? [duplicate]

This question already has answers here:
get the last picture taken by user
(3 answers)
Closed 9 years ago.
My intention is not to take a picture and then save it to sd card , get the link and all. The image is already taked with the original camera app in the android.
All i need it how can i get that image path with respect to sd card like
emulated/0/sdcard/DCIM/100ANDRO/image.jpg
how do i get that format of the recently taken image.
I've accomplished it like this:
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
imageCursor.moveToFirst();
do {
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (fullPath.contains("DCIM")) {
//--last image from camera --
return;
}
}
while (imageCursor.moveToNext());
Use this to get path of file from URI:
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Cursor provides random read-write access to the result set returned by a database query.
getContentResolver () returns a ContentResolver instance for your application's package.
When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.
MediaStore: The Media provider contains meta data for all available media on both internal and external storage devices. MediaStore.images contains meta data for all available images.

Joining contacts and photo tables in Android

I am developing a phonebook app. I am trying to retrieve contact picture. At first I retrieved all the contacts and then took each contact_id and did a query on the photo table to get the picture. However, it is taking forever to query all the contacts for pics. As in my emulator there are more than 1000 contacts, so, more than 1000 hits on the photo table is being fired. So, is there a way to join the two tables and get the data in a single query?
Below is my code to do it. But I know its wrong. Just gave it a shot. Please someone correct it.
String[] projection = new String[]{Contacts._ID, Contacts.DISPLAY_NAME};
String joinCondition = "ContactsContract.Contacts._ID=ContactsContract.CommonDataKinds.Photo.CONTACT_ID";
ContentResolver cr = context.getContentResolver();
Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
Uri fillUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cur = cr.query(contactUri, projection,
joinCondition, null, Contacts.DISPLAY_NAME);
Thx!
Rahul.
You should be retrieving the photo thumbnail in the list adapter's getView method. This is already optimized so that you only have to process the records which need to be shown on the screen. So at most you'll have to query a handful at a time, even if you have thousands of contacts.
you can try the following:
public final String[] columns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
Cursor c = contentResolver.query(Contacts.CONTENT_URI, columns, where, args, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
this should return you a cursor with all contacts and their pictures

Android getting contacts photo from data.Email query

I am making a autocomplete Field that queries contacts by Display name and Email. When someone clicks on the desired contact after the filtering that contact is added to a list with his email, display name and Picture if he has any.
So so far i have managed to do everything except to make the Photo appear. Here is how i run the query to get the email, display name, ID , and Photo ID.
return mContent.query(Email.CONTENT_URI,
PROJECTION, filter, null, null);
where projection is:
PROJECTION = new String[] {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
Email.DATA
};
This one does what i need and returns all the data. But one thing i noticed during debugging this issue is that the contact id is different than if you run the query against ContactsContract.Contacts.CONTENT_URI for a specific display name for example.
For example the tests i have run where i get all the contacts by running the Contacts.CONTENT_URI gave me a contact with an image and Id of 152. However the query against the Email.CONTENT_URI gives me an id of 452 for the same contact (With same display name and email address). so when i try to get the Photo for a content uri containing the Id 452 it returns that the photo doesnt exist, but if i try to get the photo for 152 it works perfectly.
What is causing this issue? how do i get the correct User ID? Is there any relational query that i can maybe run to get a contact ID, or maybe a correct way to get it with the help of this one.
Thank you.
EDIT
I found this digging around old code. Might be helpful to anyone.
So the full query:
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1" + " ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;
then its
getContentResolver().query( Email.CONTENT_URI, PROJECTION, filter, null, order);
When you want to access the photo of a contact, you need to specify the contact photo URI, for example using this method:
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
But for contactId you must use:
String id = ContactsContract.CommonDataKinds.Photo.CONTACT_ID;
long contactId = Long.parseLong(id);
Please note that a common error is to use ContactsContract.Contacts._ID instead ContactsContract.CommonDataKinds.Photo.CONTACT_ID
I hope that can help you.
You should use RAW_CONTACT_ID in the query. For ex, there can be two different contacts i.e. different RAW_CONTACT_ID for a single CONTACT_ID.
maybe you can take a look at this blog post in the example there they query all contacts, email addresses and the contact photo
http://blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/
best code will be
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Id);
Bitmap photoBitmap;
ContentResolver cr = getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);
photoBitmap = BitmapFactory.decodeStream(is);
it works for all

How to query Android MediaStore Content Provider, avoiding orphaned images?

I'm trying to provide an in-app Activity which displays thumbnails of photos in the
device's media store, and allow the user to select one. After the user makes a
selection, the application reads the original full-size image and does things with it.
I'm using the following code to create a Cursor over all the images on the external
storage:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.image_select );
mGridView = (GridView) findViewById( R.id.image_select_grid );
// Query for all images on external storage
String[] projection = { MediaStore.Images.Media._ID };
String selection = "";
String [] selectionArgs = null;
mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null );
// Initialize an adapter to display images in grid
if ( mImageCursor != null ) {
mImageCursor.moveToFirst();
mAdapter = new LazyCursorAdapter(this, mImageCursor, R.drawable.image_select_default);
mGridView.setAdapter( mAdapter );
} else {
Log.i(TAG, "System media store is empty.");
}
}
And the following code to load the thumbnail image (Android 2.x code is shown):
// ...
// Build URI to the main image from the cursor
int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) );
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
Integer.toString(imageID) );
loadThumbnailImage( uri.toString() );
// ...
protected Bitmap loadThumbnailImage( String url ) {
// Get original image ID
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
// Get (or create upon demand) the micro thumbnail for the original image.
return MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
And the following code to load the original image from the URL once the user makes a selection:
public Bitmap loadFullImage( Context context, Uri photoUri ) {
Cursor photoCursor = null;
try {
// Attempt to fetch asset filename for image
String[] projection = { MediaStore.Images.Media.DATA };
photoCursor = context.getContentResolver().query( photoUri,
projection, null, null, null );
if ( photoCursor != null && photoCursor.getCount() == 1 ) {
photoCursor.moveToFirst();
String photoFilePath = photoCursor.getString(
photoCursor.getColumnIndex(MediaStore.Images.Media.DATA) );
// Load image from path
return BitmapFactory.decodeFile( photoFilePath, null );
}
} finally {
if ( photoCursor != null ) {
photoCursor.close();
}
}
return null;
}
The problem I'm seeing on some Android devices, including my own personal phone, is that the
cursor I get from the query in onCreate() contains a few entries for which the actual full-sized image file (JPG or PNG) is missing. (In the case of my phone, the images had been imported and subsequently erased by iPhoto).
The orphaned entries may or may not have thumbnails, depending upon whether thumbnails where generated before the actual media file when AWOL. The end result is that the app displays thumbnails for images that don't actually exist.
I have a few questions:
Is there a query I can make to the MediaStore content provider that will filter out
images with missing media in the returned Cursor?
Is there a means, or an API to force the MediaStore to rescan, and eliminate the orphan entries? On my phone, I USB-mounted then unmounted the external media, which is supposed to trigger a rescan. But the orphan entries remain.
Or is there something fundamentally wrong with my approach that's causing this problem?
Thanks.
Okay, I've found the problem with this code sample.
In the onCreate() method, I had this line:
mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null );
The problem here is that it's querying for the thumbnails, rather than the actual images. The camera app on HTC devices does not create thumbnails by default, and so this query will fail to return images that do not already have thumbnails calculated.
Instead, query for the actual images themselves:
mImageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null );
This will return a cursor containing all the full-sized images on the system. You can then call:
Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
which will return the medium-sized thumbnail for the associated full-size image, generating it if necessary. To get the micro-sized thumbnail, just use MediaStore.Images.Thumbnails.MICRO_KIND instead.
This also solved the problem of finding thumbnails that have dangling references to the original full-sized images.
Please note that things will be changing soon, managedQuery method is deprecated. Use CursorLoader instead(since api level 11).

Categories

Resources