如何从xamarin形式的目录路径获取图片

问题描述 投票:1回答:1

我想检索图像,我有它在手机路径

filepath = "/storage/emulated/0/Android/data/com.CommunicatorEye/files/Pictures/EmployeesCards/IMG_20190131_143513.jpg";

var image = DependencyService.Get<IDependency().RetriveImageFromLocation(filepath);

IDependency.cs

 public interface IDependency
    {
        Task<Image> RetriveImageFromLocation(string location);
    }

Android的依赖Implementation.vs

public async Task<Image> RetriveImageFromLocation(string location)
        {
            Image image = new Image();
            var memoryStream = new MemoryStream();

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

            image.Source = ImageSource.FromStream(() => memoryStream);
            return image;
        }

但它不工作对我来说,任何样品?

xamarin xamarin.forms
1个回答
1
投票

如果该文件是您的应用程序的沙盒中,没有理由使用DI / DependencyService /等等,以获得流来填充ImageSource然后添加到Image

使用一个FileImageSource(静态ImageSource.FromFile),并提供它的路径:

var image = new Image
{
    Source = ImageSource.FromFile(filePath)
};
© www.soinside.com 2019 - 2024. All rights reserved.