WinRT:如何通过URI从图片库中读取图片?

时间:2015-05-14 19:04:17

标签: windows-phone-8 windows-store-apps

尝试通过URI读取存储在图片库中的图像,从不显示图像(在图像控件中)。通过流读取相同的图像(假设应用程序已经声明了图片库功能)。通过URI从应用程序的数据文件夹中读取图像。

有人知道可能出现什么问题吗?

以下是我(不成功)尝试通过URI读取图像的方式:

var imageFile = (await KnownFolders.PicturesLibrary.GetFilesAsync()).FirstOrDefault();
string imagePath = imageFile.Path;
Uri uriSource = new Uri(imagePath);
var bitmap = new BitmapImage(uriSource);
this.Image.Source = bitmap;

以下是我通过流程成功阅读相同图片的方式:

var imageFile = (await KnownFolders.PicturesLibrary.GetFilesAsync()).FirstOrDefault();
BitmapImage bitmap;
using (var stream = await imageFile.OpenReadAsync())
{
   bitmap = new BitmapImage();
   await bitmap.SetSourceAsync(stream);
}
this.Image.Source = bitmap;

我需要通过URI读取图像,因为这是读取图像的最快方式,本质上是异步的,与数据绑定完美配合。

1 个答案:

答案 0 :(得分:2)

图片库没有URI。您需要获取StorageFile并将其流入。

您使用的文件URI不起作用,因为该应用无法直接访问PicturesLibrary,因此无法通过路径引用该项目。 StorageFile对象提供对应用程序本身没有权限的位置的代理访问。

相关问题