繁体   English   中英

如何在Xamarin.Forms中显示设备存储中的图像?

[英]How to show images from device storage in Xamarin.Forms?

我需要将图像的Source属性设置为用户从画廊或相机(使用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));

但是图像什么也没显示

您可以创建MediaFile对象,例如

public MediaFile AttachedImage;

并将捕获的图像存储到其中,以便解决以下问题,将引发此异常:System.ObjectDisposedException:无法访问已处置的对象。 对象名称:'流已关闭 您可以随时从该对象获取图像流,例如,

img.Source = AttachedImage.GetStream().

它一定会解决您的问题。

Media Plugin返回带有路径的文件。 您必须使用此文件:

image.Source = ImageSource.FromStream(() =>
    {
        var stream = file.GetStream();
        return stream;
    }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM