如何使用 Xamarin-Forms 下载图像并将其保存在本地存储中.? [英] How to download image and save it in local storage using Xamarin-Forms.?

查看:31
本文介绍了如何使用 Xamarin-Forms 下载图像并将其保存在本地存储中.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一张图片并将其存储在本地存储的特定文件夹中.

I want to download an image and store it in specific folder in local storage.

我用它来下载图片:

var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename);
var img = ImageSource.FromStream(() => new MemoryStream(imageData));

推荐答案

创建 FileService 接口

在您的共享代码中,创建一个新接口,例如,名为 IFileService.cs

in your Shared Code, create a new Interface, for instance, called IFileService.cs

 public interface IFileService
 {
      void SavePicture(string name, Stream data, string location="temp");
 }

实现安卓

在您的 android 项目中,创建一个名为Fileservice.cs"的新类.

In your android project, create a new class called "Fileservice.cs".

确保它派生自你之前创建的接口并用依赖信息装饰它:

Make sure it derives from your interface created before and decorate it with the dependency information:

[assembly: Dependency(typeof(FileService))]
namespace MyApp.Droid
{
    public class FileService : IFileService
    {
        public void SavePicture(string name, Stream data, string location = "temp")
        {
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            documentsPath = Path.Combine(documentsPath, "Orders", location);
            Directory.CreateDirectory(documentsPath);

            string filePath = Path.Combine(documentsPath, name);

            byte[] bArray = new byte[data.Length];
            using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
            {
                using (data)
                {
                    data.Read(bArray, 0, (int)data.Length);
                }
                int length = bArray.Length;
                fs.Write(bArray, 0, length);
            }
        }
    }
}

实现 iOSiOS的实现基本相同:

Implementation iOS The implementation for iOS is basically the same:

[assembly: Dependency(typeof(FileService))]
namespace MyApp.iOS
{
    public class FileService: IFileService
    {
        public void SavePicture(string name, Stream data, string location = "temp")
        {
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            documentsPath = Path.Combine(documentsPath, "Orders", location);
            Directory.CreateDirectory(documentsPath);

            string filePath = Path.Combine(documentsPath, name);

            byte[] bArray = new byte[data.Length];
            using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
            {
                using (data)
                {
                    data.Read(bArray, 0, (int)data.Length);
                }
                int length = bArray.Length;
                fs.Write(bArray, 0, length);
            }
        }
    }
}

为了保存您的文件,在您的共享代码中,您调用

In order to save your file, in your shared code, you call

DependencyService.Get<IFileService>().SavePicture("ImageName.jpg", imageData, "imagesFolder");

应该很顺利.

这篇关于如何使用 Xamarin-Forms 下载图像并将其保存在本地存储中.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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