WP8在MediaLibrary中获取图像的URI

时间:2014-02-26 11:14:56

标签: windows-phone-8 uri longlistselector media-library

当我使用自己的LongListSelector让用户从图像中选择时,我需要检索Medialibrary中所有图像的URI。我找不到任何可能做到这一点。

我所看到的只是获得图像的名称:

MediaLibrary m = new MediaLibrary();

foreach (var r in m.Pictures)
{
    Stream imageStream = r.GetImage();
}

我如何获得剩下的路径?

修改

遵循第一个解决方案:

Gallery.xaml

 <phone:LongListSelector 
                x:Name="GalleryLLS"
                LayoutMode="Grid" 
                GridCellSize="108,108"
                SelectionChanged="GalleryLLS_SelectionChanged"
                Margin="0,0,144,12"
                ItemsSource="{Binding ListOfImages}" >
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Image Width="150" Height="150" 
                                Source="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

Gallery.xaml.cs

    private List<WriteableBitmap> _listOfImages = new List<WriteableBitmap>();

    public List<WriteableBitmap> ListOfImages
    {
        get { return _listOfImages; }

        set { _listOfImages = value; }
    }

    public Gallery()
    {
        InitializeComponent();

        var ml = new MediaLibrary();
        var Pictures = ml.Pictures;
        foreach (var item in Pictures)
        {
            ListOfImages.Add(PictureDecoder.DecodeJpeg(item.GetImage()));
        }

    }

导致无法显示图像。调试器显示imageas已正确添加到我的列表中,但我什么都看不到。

2 个答案:

答案 0 :(得分:2)

为什么你需要这条路?要显示图像?如果您只需要将列表中的图像显示为缩略图,则可以使用该流创建BitmapImage

var bi = new BitmapImage();
bi.SetSource(r.GetThumbnail());

现在,您可以将bi设置为Image.Source

答案 1 :(得分:2)

似乎API没有公开从媒体库返回的图片的URI属性。所以你需要用不同的方法来做。例如,您可以拥有WritableBitmap的列表而不是URI列表:

private List<WriteableBitmap> _listOfImages = new List<WriteableBitmap>();
public List<WriteableBitmap> ListOfImages
{
    get { return _listOfImages; }

    set { _listOfImages = value; }
}
.......
.......
var ml = new MediaLibrary();            
var Pictures = ml.Pictures;
foreach (var item in Pictures)
{
    ListOfImages.Add(PictureDecoder.DecodeJpeg(item.GetImage()));
}
........
//in XAML
<phone:LongListSelector ItemsSource="{Binding ListOfImages}"> 
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Width="150" Height="150" 
                                Source="{Binding}"/> 
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
........
</phone:LongListSelector>
........

[代码改编自http://www.neelesh-vishwakarma.com]