微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 获取联系人图片Xamarin Forms

我正在使用Xamarin Forms创建一个混合移动应用程序.我想显示所有电话簿联系人的列表,其中包含以下详细信息:

>姓名
>图片

我在android清单中添加了“READ_CONTACTS”权限.以下是获取所有联系人的代码

var contactList = new List < MContacts > ();

var ContactDetailURI = ContactsContract.Contacts.ContentUri;

string[] ContactDetailProjection = {
 ContactsContract.Contacts.InterfaceConsts.Id,ContactsContract.Contacts.InterfaceConsts.displayName,ContactsContract.ContactsColumns.ContactLastUpdatedTimestamp,ContactsContract.Contacts.InterfaceConsts.PhotoId,ContactsContract.Contacts.InterfaceConsts.PhotoUri,ContactsContract.Contacts.InterfaceConsts.PhotoFileId,ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
};

var ContactDetailCursor = Forms.Context.ContentResolver.Query(
 ContactDetailURI,ContactDetailProjection,null,null
);

if (ContactDetailCursor.MovetoFirst()) {
 do {
    var contact = new MContacts();
    contact.Id = ContactDetailCursor.GetLong(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[0]));
    contact.displayName = ContactDetailCursor.GetString(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[1]));
    contact.PhotoId = ContactDetailCursor.GetString(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[6]));

    contactList.Add(contact);

 } while (ContactDetailCursor.MovetoNext());
}

return contactList;

我有一个XAML页面,它将显示数据.我正在使用Image Cell.以下是XAML代码

displayName}"
                    ImageSource="{Binding PhotoId}">
                

我收到了联系人姓名,但在图片领域我只得到这个:

content://com.android.contacts/contacts/1/photo

我该怎么做才能显示图像?我在这里错过了什么?

附:我不想使用Xamarin.Mobile组件或任何其他组件.

最佳答案
Android可以通过内容提供商和内容解析器访问其他应用程序中的文件.因此,您首先必须将该内容:// scheme URL转换为文件流.你可以这样做:

// convert uri to stream (Android code,ContentResolver is a property of the Activity class):
var stream = ContentResolver.OpenInputStream(uri);

// or when not in an activity (e.g. a service):
var otherStream = Android.App.Application.Context.ContentResolver.OpenInputStream(uri);

// eventually convert the stream to imagesource for consumption in Xamarin Forms:
var imagesource = Xamarin.Forms.ImageSource.FromStream(() => stream);

有关更多信息,请参阅以下资源:

> https://developer.xamarin.com/guides/android/platform_features/intro_to_content_providers/
> https://developer.android.com/guide/topics/providers/content-providers.html

原文地址:https://www.jb51.cc/android/430119.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐