WP8 get URI of images in MediaLibrary
-
21-12-2019 - |
Question
As I am using my own LongListSelector to let the user select from an image I need to retrieve the URI's of all images in the Medialibrary
. I couldn't find any possibility to do this yet.
All I have seen what is possible is to get the name of the image:
MediaLibrary m = new MediaLibrary();
foreach (var r in m.Pictures)
{
Stream imageStream = r.GetImage();
}
How can I get the rest of the path?
EDIT
Following the first solution:
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()));
}
}
results in not showing images. Debugger shows that imageas are added correctly to my list but I won't see anything.
Solution
It seems that the API doesn't exposes URI property for pictures returned from Media Library. So you need to do it with different approach. For example, you can have a list of WritableBitmap
s instead of list of URIs :
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>
........
[Codes adapted from http://www.neelesh-vishwakarma.com]
OTHER TIPS
Why do you need the path? To show the image? If you just need to show the image in the list as a thumbnail, you can use the stream to create a BitmapImage
:
var bi = new BitmapImage();
bi.SetSource(r.GetThumbnail());
Now you can set that bi
as a Image.Source
.