質問

From API 14 (Android 4.0 onwards) onwards I can use ContactsContract.Profile.CONTENT_URI to get the phone owner's contact profile Uri, and through that obtain their avatar/contact photo.

I want to know how to do this from API 8 (Android 2.2) through to API 13. I only need the photo (so it's okay if there is no concept of a user profile contact prior to API 14), although I'm by no means certain that it is actually possible.

役に立ちましたか?

解決

Pre API 14, there is no concept of the user profile prior to API 14.

That means it's not simply a case of finding an alternative to replace ContactsContract.Profile.CONTENT_URI with when running on pre 4.0 devices. You really have to implement a workaround.

Poor solution (A): Use the SIM phone number

If the user has added themselves as a contact, that contact info (including their photo/avatar) can be looked up by number, just like any other contact can be. The user's phone number can be retrieved from the SIM using

((TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();

Once you have the number, you can look it up using a query, e.g.

// Use PhoneLookup.PHOTO_URI from API 11 onwards for more direct lookup
final String[] proj = { PhoneLookup.DISPLAY_NAME, 
                        PhoneLookup._ID,
                        PhoneLookup.PHOTO_ID };
Cursor c = cr.query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                                         Uri.encode(number)),
                    proj, null, null, PhoneLookup.DISPLAY_NAME);

After you walk the cursor and retrieve the appropriate PHOTO_ID or PHOTO_URI, provided it exists, you can get build the proper URI like so:

// I assume local variables long photoId or String photoUriString
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI,
                                          photoId);
// Just use Uri.parse(photoUriString); from API 11 onwards     

However, there are a few problems with this method:

  • Not all operators store the phone number in their SIMs.
  • Not all phones can write to that section of the SIM.
  • Not all phones that write to that section will do so in a format usable by Android.

Slightly better solution (B): Add a user avatar option to your application

Implement a way for a user to select an image as their profile photo/avatar as an option or setting in your application. This is reasonable straight forward to implement, but forces the user to manually add an avatar if they want one in your app.

I don't want to reinvent the wheel, so here's a good starting point for selecting an image. You should save an appropriately sized version of the image rather than just the URI returned by user selection in case the source image is changed, moved, or deleted.

You may want to allow this functionality in API 14 and onwards devices as well, because it allows the user to have a different avatar specific to your application.

Alternative solution (C): A and B

Why bother? Why not just B?

Consider doing both because some users whose SIM cards contain their phone number may have added themselves as a contact (I believe the 2.3 SMS app tries the SIM number lookup), and I'm sure they will greatly appreciate not having to manually set an avatar. Making users do extra work when it could be done automatically isn't a good thing.


I added A initially just to get some functionality for older devices we were testing on, then added B as well a little later.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top