Reputation: 43
I'm developing an App for Android and UWP. In one screen I need to load an image from the device-filesystem and display it. Strangely it works perfectly on Android, but not on UWP. The Paths seem to be returned correctly by the FilePicker...
My XAML
<Image Grid.Row="0" Grid.Column="0"
Source="{Binding NewImage}" Margin="10,10,10,10" Aspect="AspectFit"
VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
The source i use for the image
private FileImageSource _newImage;
public FileImageSource NewImage
{
get { return _newImage; }
set
{
_newImage = value;
OnPropertyChanged(nameof(NewImage));
}
}
The function which is supposed to load the image and set the path.
private async void OnAddImage()
{
string[] types = { ".jpg", ".png" };
FileData temp = await CrossFilePicker.Current.PickFile(types);
if (temp == null)
{
return;
}
Debug.WriteLine($"ImagePath: {temp.FilePath}");
Debug.WriteLine($"ImageName: {temp.FileName}");
NewEntry.ImagePath = temp.FilePath;
NewImage = (FileImageSource)ImageSource.FromFile(temp.FilePath);
}
Upvotes: 1
Views: 410
Reputation: 6643
When you used file picker to pick images on UWP, these images may be stored on the external disk. We can only use a path to load files embedded in the project or stored in the local storage on UWP: https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/
So adjust your loading event like:
string[] types = { ".jpg", ".png" };
FileData temp = await CrossFilePicker.Current.PickFile(types);
if (temp == null)
{
return;
}
Debug.WriteLine($"ImagePath: {temp.FilePath}");
Debug.WriteLine($"ImageName: {temp.FileName}");
//NewEntry.ImagePath = temp.FilePath;
NewImage = (StreamImageSource)ImageSource.FromStream(() => temp.GetStream());
// Property
private StreamImageSource _newImage;
public StreamImageSource NewImage
{
get { return _newImage; }
set
{
_newImage = value;
OnPropertyChanged(nameof(NewImage));
}
}
This is also adapted on Android.
Upvotes: 1