在库中添加照片以便在wp8.1中联系

时间:2015-04-19 14:22:15

标签: c# windows-phone windows-phone-8.1 contact

在我的Windows手机应用程序中,我正在尝试创建一个新的联系人并添加从相机拍摄的图像,但它似乎不起作用。 无论我做什么,照片都是空白的。 它对我有用的唯一方法是使用我在资源文件夹中添加的图像(而不是从相机中添加),甚至尝试将图像添加到本地资源文件夹然后上传它 - 不起作用...

(没有错误,但添加的联系人没有照片)。

await contact.SetDisplayPictureAsync(stream.AsStream().AsInputStream());

这是我的代码:
当我从商店获取所选图像时,我将其保存到bitmapImage并使用其像素缓冲区。

public async void AddContact(string displayName, string mobile, string email, byte[] data)
        {
            var store = await ContactStore.CreateOrOpenAsync();
            var contact = new StoredContact(store)
            {
                DisplayName = displayName
            };

            var props = await contact.GetPropertiesAsync();
            props.Add(KnownContactProperties.Email, email);
            props.Add(KnownContactProperties.MobileTelephone, mobile);

            using (var stream = bitmap.PixelBuffer.AsStream())
            {
                await contact.SetDisplayPictureAsync(stream.AsInputStream());
            }
            await contact.SaveAsync();
        }

请帮忙!

1 个答案:

答案 0 :(得分:1)

我正在做同样的事情来设置个人资料图片以便联系,但它无法正常工作。但是当我从存储文件中获取IRandomAccessStream时,它在这里工作就是我正在做的事情

                var file = await folder.GetFileAsync("filename.jpeg"));
               //Or you can get file direclty from localfolder
               // var file = await ApplicationData.Current.LocalFolder.GetFileAsync("filename.jpeg");
                using (Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    await contact.SetDisplayPictureAsync(fileStream);
                }
                await contact.SaveAsync();

修改 如何使用媒体捕获将图片保存到本地存储。

 ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();

        //Save file to local storage
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                "MyPhoto.jpg", CreationCollisionOption.ReplaceExisting);

        await mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, file);

将图像保存在本地存储中后,您可以从我的第一个示例中获取该图像

编辑2 如果您使用的是文件打开选择器,可以试试这个。

 public async void ContinueFileOpenPicker(Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs args)
    {
        if (args.Files.Count > 0)
        {
            var filePath  = args.Files[0].Path;
            StorageFile file = await Windows.Storage.StorageFile.GetFileFromPathAsync(filePath);

            using (Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                await contact.SetDisplayPictureAsync(fileStream);
            }
       }