C# 如何以Xamarin.Forms显示设备存储中的图像?
我需要将图像的C# 如何以Xamarin.Forms显示设备存储中的图像?,c#,xamarin,xamarin.forms,imagesource,C#,Xamarin,Xamarin.forms,Imagesource,我需要将图像的Source属性设置为用户从gallery或camera中拾取图像后返回的路径(使用Plugin.Media) 路径如下所示: /storage/emulated/0/Android/data/com.company.myApp/files/Pictures/temp/Screenshot_20181225-134128_6.jpg 我试过这个 ImageSource.FromUri(new Uri(file.FilePath, UriKind.RelativeOrAbsolut
Source
属性设置为用户从gallery或camera中拾取图像后返回的路径(使用Plugin.Media)
路径如下所示:
/storage/emulated/0/Android/data/com.company.myApp/files/Pictures/temp/Screenshot_20181225-134128_6.jpg
我试过这个
ImageSource.FromUri(new Uri(file.FilePath, UriKind.RelativeOrAbsolute));
但是图像没有显示任何内容,媒体插件返回一个带有路径的文件。对于文件,您必须使用此选项:
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
您可以创建MediaFile的对象,如
公共媒体文件附件图像代码>
并将捕获的图像存储到其中,以便解决以下问题,
引发此异常:System.ObjectDisposedException:无法访问已处置对象。对象名称:'流已关闭。
您可以随时从该对象获取图像流,如
img.Source = AttachedImage.GetStream().
它肯定会解决您的问题。控制台或logcat中有任何异常吗?您是否有外部文件的权限:否,我不想使用FromStream
,不适合数据绑定。您可以在视图中使用它填充包含ImageSource的模型,因此数据绑定将正常。引发此异常:System.ObjectDisposedException:无法访问已处置对象。对象名称:“流已关闭”。将文件流复制到MemoryStream并从此处使用它。