将Image.Source设置为Xamarin.Forms中外部存储中的文件 [英] Set Image.Source to file in external storage in Xamarin.Forms

查看:551
本文介绍了将Image.Source设置为Xamarin.Forms中外部存储中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在外部存储中有一张照片(通过意图保存在我的应用中).我想在共享项目的Image视图中显示此图片.

I have a picture of item in external storage (that was saved by intent in my app). I want to display this picture in Image view in my shared project.

Image.Source接受ImageSource类型的对象.我尝试了ImageSource.FromFileImageSource.FromStream甚至ImageSource.FromUri.结果始终是不显示图像(没有错误或异常).通过首先在上面的File.Open行中打开文件,我验证了文件路径是否正确.

Image.Source takes object of ImageSource type. I tried ImageSource.FromFile, ImageSource.FromStream and even ImageSource.FromUri. The result is always that image is not displayed (no error or exception). I validated that the path to file is correct by first opening it with File.Open one line above.

从普通存储而不是从资产/资源/等显示图片的正确方法是什么?

What is the correct way of displaying pictures from normal storage, not from assets/resources/etc?

此代码不起作用:

var path = "/storage/emulated/0/Pictures/6afbd8c6-bb1e-49d3-838c-0fa809e97cf1.jpg" //in real app the path is taken from DB
var image = new Image() {Aspect = Aspect.AspectFit, WidthRequest = 200, HeightRequest = 200};
image.Source = ImageSource.FromFile(path);

推荐答案

您的Xamarin表单PCL由于特定于其平台,因此不知道Android的URI是什么,所以:

Your Xamarin Forms PCL don't know what its a URI from Android beacuse its platform specific, so:

ImageSource.FromFile(path);

不起作用.

在这种情况下,您要处理特定于平台的功能,即从Android加载图像. 我建议这种方法:

In that case you are handling platform specific features, that is loading an image from Android. I suggest this approach:

在Xamarin Forms PCL上创建一个界面,例如:

Create an interface on Xamarin Forms PCL like:

public interface IPhoto
{
    Task<Stream> GetPhoto ();
}

然后在Android中实现该接口并在DependencyService中注册实现:

Then in Android you implement that interface and register the implementation in DependencyService:

[assembly: Xamarin.Forms.Dependency(typeof(PhotoImplementation))]
    namespace xpto
    {
        public class PhotoImplementation : Java.Lang.Object, IPhoto
        {
            public async Task<Stream> GetPhoto()
            {
                // Open the photo and put it in a Stream to return       
                var memoryStream = new MemoryStream();

                using (var source = System.IO.File.OpenRead(path))
                {
                    await source.CopyToAsync(memoryStream);
                }

                return memoryStream;
            }
        }
    }

在Xamarin Forms PCL代码中获取图像:

In the Xamarin Forms PCL code get the image:

var image = ImageSource.FromStream ( () => await DependencyService.Get<IPhoto>().GetPhoto());

有关更多详细信息,请查阅.

For more detail you can consult this.

注意1:如果您也实现了IPhoto接口,则该功能将在iOS上运行.

注2:Xamarin-Forms-Labs中存在一个用于此类功能的有用的库,称为相机.

NOTE2: There exist a helpful library for this kind of features from Xamarin-Forms-Labs called Camera.

更新(共享项目解决方案)

根据注释中的要求,要在共享项目中使用它而不是PCL,我们可以这样做.

As requested in the comments, to use this in a Shared Project instead of PCL we could do this.

1-将IPhotoInterface放置在共享项目中.

1 - Place the IPhotoInterface in the Shared Project.

2-在Android/iOS项目中实现界面:

2 - Implement the interface in Android/iOS project:

public class PhotoImplementation : IPhoto
{
    public async Task<Stream> GetPhoto()
    {
        // Open the photo and put it in a Stream to return. 
    }
}

3-在共享项目中使用它:

3 - Use it in the Shared Project:

IPhoto iPhotoImplementation;

#if __ANDROID__
iPhotoImplementation = new shared_native.Droid.GetPicture();

#elif __IOS__
iPhotoImplementation = new shared_native.iOS.GetPicture();

#endif

var image = ImageSource.FromStream ( () => await iPhotoImplementation.GetPhoto());

注意:shared_native是我的解决方案的名称空间,Droid/iOS是Android和iOS的项目.

NOTE: shared_native is the namespace of my solution and Droid/iOS are the projects for Android and iOS.

这篇关于将Image.Source设置为Xamarin.Forms中外部存储中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆