【问题标题】:Get Contact Image Xamarin Forms获取联系人图片 Xamarin 表单
【发布时间】:2017-10-01 01:07:54
【问题描述】:

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

  1. 姓名
  2. 图片

我在 android manifest 中添加了“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,
 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 页面。我正在使用图像单元。下面是 XAML 代码:

<ContentPage.Content>
    <ListView x:Name="ContactList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell
                    Text="{Binding DisplayName}"
                    ImageSource="{Binding PhotoId}">
                </ImageCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

我得到了联系人姓名,但在图像字段中我只得到这个:

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

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

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

【问题讨论】:

    标签: android xamarin xamarin.forms android-contacts


    【解决方案1】:

    Android 通过 Content Providers 和 Content Resolvers 访问来自其他应用程序的文件。因此,您首先必须将 content:// 方案 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);
    

    有关详细信息,请参阅以下资源:

    【讨论】:

    • 出现错误:“访问非静态字段方法或属性 ContentResolver.OpenInputStream 需要对象引用”我的 uri 是:var uri = Android.Net.Uri.Parse(new System.Uri (ContactDetailCursor.GetString(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[4]))).ToString());
    • 您是否使用了Android Activity 类的ContentResolver 属性?还是来自其他地方?我刚刚用更多信息更新了答案,以防您在 Activity 之外调用它,在这种情况下,您必须通过应用程序 Context
    • 是的,使用“otherStream”解决了这个问题:) 但是最后一个问题,现在当我写“contact.PhotoId = imagesource;”时我收到一条错误消息,提示“无法将 Xamarin.Forms.ImageSource 类型隐式转换为字符串”
    • 因为Contact 类上的PhotoId 属性可能是string 而不是ImageSource 类型。
    • 解决了我的问题。谢谢:)
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多