android - 无法检索用户个人资料
问题描述
我想检索用户的个人资料及其图像,但这不起作用。我总是得到一个空光标(cursor.getCount() == 0
)。有人可以帮忙吗?
我的手机上有一张带有图片和电话号码的个人资料,但我无法阅读。授予权限(读取和写入联系人权限),我可以检索我所有的电话联系人,但不能检索自己的个人资料。
有任何想法吗?
代码
void loadUser() {
Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
String[] selection = new String[]
{
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data._ID,
ContactsContract.Profile.DISPLAY_NAME,
ContactsContract.Profile.PHOTO_URI,
ContactsContract.Profile.LOOKUP_KEY,
ContactsContract.Data.DATA_VERSION
};
Cursor cursor = MainApp.get().getContentResolver().query(
dataUri,
selection,
null,
null,
null);
if (cursor != null) {
L.d("MY PROFILE - cursor size: %d", cursor.getCount());
int rawId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
int id = cursor.getColumnIndex(ContactsContract.Data._ID);
int name = cursor.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME);
int photoUri = cursor.getColumnIndex(ContactsContract.Profile.PHOTO_URI);
int lookupKey = cursor.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY);
int version = cursor.getColumnIndex(ContactsContract.Data.DATA_VERSION);
try {
if (cursor.moveToFirst()) {
long phRawId = cursor.getLong(rawId);
int phId = cursor.getInt(id);
String phName = cursor.getString(name);
String phImageUri = cursor.getString(photoUri);
String phLookupKey = cursor.getString(lookupKey);
int phVersion = cursor.getInt(version);
boolean phExists = true;
L.d("MY PROFILE - RawID: %d, ID: %d", phRawId, phId);
// ... profile successfully retrieved
} else {
L.d("MY PROFILE - cursor is EMPTY");
}
} finally {
cursor.close();
}
} else {
L.d("MY PROFILE - cursor = NULL");
}
}
附加信息
我认为这段代码在我的带有 android 7 的 S6 上工作,但它不能在我的带有 android 8 的新 S9 上工作(不能再在我的旧手机上测试它,因为它不再工作了)。所以这可能是一个特定于android版本的问题......
解决方案
这似乎是三星联系人应用程序的错误实施,我在他们的开发者论坛上打开了一个错误报告:https ://developer.samsung.com/forum/thread/contacts-app-profile-is-not-accessible- via-contactscontractprofile-api/201/354874
推荐阅读
- javascript - 如何控制台记录一个承诺?
- vue.js - 带有图像的 nuxtjs 静态站点生成
- laravel - 使用 Laravel 使用 HTTP 客户端 API 发送动态变量
- r - 有效地重命名/重新编码变量
- javascript - Webpack,优化分块给出“冲突:多个块将资产发送到相同的文件名”错误
- python - Python 请求响应 - 如果 elif 条件不起作用
- air - 调试从 TestFlight 下载的 Adobe AIR iOS 应用程序
- azure - 来自 Azure 日志应用程序 + Azure 队列的排队消息会发生什么情况?
- visual-studio-code - 在设置中将“useCodeSnippetsOnFunctionSuggest”设置为true。函数自动完成后JSON不完成括号
- iterator - WSO2 迭代消息并将消息发送到消息存储