简体   繁体   English

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

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

I need to set the Source property of an image to the path returned after the user has picked it from gallery or camera (using Plugin.Media) the path looks like this: 我需要将图像的Source属性设置为用户从画廊或相机(使用Plugin.Media)中选择图像后返回的路径,该路径如下所示:

/storage/emulated/0/Android/data/com.company.myApp/files/Pictures/temp/Screenshot_20181225-134128_6.jpg

I tried this 我试过了

ImageSource.FromUri(new Uri(file.FilePath, UriKind.RelativeOrAbsolute));

but the image doesn't show anything, 但是图像什么也没显示

you can create an object of MediaFile like, 您可以创建MediaFile对象,例如

public MediaFile AttachedImage;

and store captured image into it so that your below issue will resolved, this exception is thrown: System.ObjectDisposedException: Cannot access a disposed object. 并将捕获的图像存储到其中,以便解决以下问题,将引发此异常:System.ObjectDisposedException:无法访问已处置的对象。 Object name: 'Stream has been closed . 对象名称:'流已关闭 you can get Image Stream any time from that object like, 您可以随时从该对象获取图像流,例如,

img.Source = AttachedImage.GetStream().

it will sure resolve your issue. 它一定会解决您的问题。

Media Plugin returns a file with the path. Media Plugin返回带有路径的文件。 You have to use this, for the file: 您必须使用此文件:

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

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

相关问题 如何在 Xamarin.Forms 中将非图像文件(pdf、pptx、docx)从 FirebaseStorage 下载到设备的内部存储? - How to download non-image file(pdf,pptx,docx) from FirebaseStorage to internal storage of device in Xamarin.Forms? 使用 Xamarin.Forms 显示来自 URL 的图像 - Show image from URL with Xamarin.Forms xamarin.forms如何在标题栏中显示视图 - xamarin.forms how to show a view in the titlebar 如何使图像显示在我的 Xamarin.Forms 应用程序的 UWP 版本中 - How do I make images show up in UWP version of my Xamarin.Forms application Xamarin.Forms缓存图像 - Xamarin.Forms caching images Xamarin.Forms图像无法渲染 - Xamarin.Forms images not rendering Xamarin.Forms MediaManager 从本地存储播放视频 - Xamarin.Forms MediaManager play video from local storage 在 Xamarin.Forms 中 Device.BeginInvokeOnMainThread() 不显示来自通知回调的消息框*仅*在物理设备上的发布配置中 - In Xamarin.Forms Device.BeginInvokeOnMainThread() doesn’t show message box from notification callback *only* in Release config on physical device 如何在Xamarin.Forms中获取客户端设备的MAC地址? - How to get MAC address of a client device in Xamarin.Forms? Xamarin.Forms:如何使用独立于设备语言的本地化 - Xamarin.Forms : How to use localization independent of device language
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM