获取用户/所有者的个人资料联系URI和用户形象与API 8日起 [英] Get user/owner profile contact URI and user image with API 8 onwards

查看:173
本文介绍了获取用户/所有者的个人资料联系URI和用户形象与API 8日起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从API 14(Android 4.0的起)起,我可以使用 ContactsContract.Profile.CONTENT_URI 来获得手机主人的与轮廓URI和通过获得他们的头像/联系人的照片。

我想知道如何从API 8(Android 2.2的),通过这样对API 13.我只的需求的照片(所以它的好,如果没有用户配置文件接触的概念之前,以API 14),虽然我不能肯定,它实际上是可能的。

解决方案

pre的API 14,不存在用户简档的概念之前的API 14。

这意味着它不只是找到一个替代品取代 ContactsContract.Profile.CONTENT_URI 上pre 4.0设备上运行时的情况。你真的要实现一个解决方法。

可怜的溶液(A):使用SIM卡的电话号码

如果用户已添加自己为联系人,该联系人信息(包括他们的照片/头像),可以按编号抬头一看,就像任何其他接触即可。用户的电话号码可以从SIM使用检索

<$p$p><$c$c>((TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();

一旦你的号码,你可以看看它使用查询,例如

  //使用PhoneLookup.PHOTO_URI从API 11日起更直接查找
最终的String [] = PROJ {PhoneLookup.DISPLAY_NAME,
                        PhoneLookup._ID,
                        PhoneLookup.PHOTO_ID};
光标C = cr.query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                                         Uri.en code(号)),
                    PROJ,NULL,NULL,PhoneLookup.DISPLAY_NAME);

你走光标和检索后,相应的 PHOTO_ID PHOTO_URI ,只要它存在,你可以得到建正确的URI,像这样:

  //我假设局部变量长PHOTOID或字符串photoUriString
乌里photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI,
                                          PHOTOID);
//只需使用Uri.parse(photoUriString);从API 11日起

然而,有这种方法的几个问题:


  • 并非所有的运营商存储在他们的模拟市民的电话号码。

  • 并非所有的手机都可以写入SIM的那部分。

  • 这并不是说写的那款手机都将搭载Android使用的格式这么做。

稍好溶液(B):添加头像选项,您的应用程序

实现为用户选择图像个人资料照片/化身为你的应用程序选项或设置的一种方式。这是实现合理的直线前进,但强制用户,如果他们想要一个手动添加一个化身的在您的应用程序。

我不希望推倒重来,所以这里有一个<一个href=\"http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android\">good起点选择图像。你应该保存图像的大小合适的版本,而不是仅仅通过的情况下,用户选择的源图像改变返回的URI,移动或删除。

您可能要允许在14 API此功能,并开始设备为好,因为它允许用户有特定于应用程序不同的头像。

替代溶液(C):A和B

何必呢?为什么不B地?

考虑做一方面是因为有些用户的SIM卡包含他们的电话号码可能已经添加了自己作为一个接触(我相信2.3的短信应用试图SIM卡号码查询),我敢肯定他们会大大AP preciate不必手动设置的化身。让用户做的时候它可以自动完成是不是一件好事额外的工作。


我添加了一个最初只是得到的部分的为我们在测试旧设备上的功能,则含有B以及稍晚。

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.

这篇关于获取用户/所有者的个人资料联系URI和用户形象与API 8日起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆