Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何下载图像并使用Xamarin表单将其保存在本地存储中。?_C#_Xamarin.forms - Fatal编程技术网

C# 如何下载图像并使用Xamarin表单将其保存在本地存储中。?

C# 如何下载图像并使用Xamarin表单将其保存在本地存储中。?,c#,xamarin.forms,C#,Xamarin.forms,我想下载一个图像并将其存储在本地存储的特定文件夹中 我正在使用此文件下载图像: var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename); var img = ImageSource.FromStream(() => new MemoryStream(imageData)); 在这里,您必须使用xamarin forms PCL的依赖项服务从android项目调用此方法。这

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

我正在使用此文件下载图像:

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

在这里,您必须使用xamarin forms PCL的依赖项服务从android项目调用此方法。这将把您的图像存储到公用文件夹中。如果我有时间用iOS做一个演示,我会编辑这个

创建文件服务接口

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

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

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

确保它源自之前创建的接口,并用依赖项信息修饰它:

[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);
            }
        }
    }
}
实施iOS iOS的实现基本相同:

[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);
            }
        }
    }
}
为了保存文件,在共享代码中调用

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

而且应该可以走了。

我真的很喜欢卡兰的方法

我把它们结合在一起,我想在这里分享。实际上工作得很好

public String DownloadImage(Uri URL)
{
    WebClient webClient = new WebClient();

    string folderPath   = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Images", "temp");
    string fileName     = URL.ToString().Split('/').Last();
    string filePath     = System.IO.Path.Combine(folderPath, fileName);

    webClient.DownloadDataCompleted += (s, e) =>
    {
        Directory.CreateDirectory(folderPath);

        File.WriteAllBytes(filePath, e.Result);
    };

    webClient.DownloadDataAsync(URL);

    return filePath;
}

你尝试过什么?这到底是怎么不起作用的?请提供一个答案,否则我们可能无法帮助你。我不知道怎么走。我该怎么办。。。。我有一个Android链接,但我如何在xamarin.forms中实现,请先阅读xamarin.forms中的文件处理。这应该给你一个好的起点。我试图编辑awnser,但不能只编辑1个字符,在共享代码中,您得到的是DependencyService上的接口,而不是项目实现类DependencyService.get().SavePicture(“ImageName.jpg”,imageData,“imagesFolder”);它应该自动创建一个名为“temp”的文件夹,还是将图像保存在哪里?我运行了代码,并对其进行了编译,但我找不到我保存的图像。像这样的书都可以从共享项目中完成,不是吗?
public String DownloadImage(Uri URL)
{
    WebClient webClient = new WebClient();

    string folderPath   = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Images", "temp");
    string fileName     = URL.ToString().Split('/').Last();
    string filePath     = System.IO.Path.Combine(folderPath, fileName);

    webClient.DownloadDataCompleted += (s, e) =>
    {
        Directory.CreateDirectory(folderPath);

        File.WriteAllBytes(filePath, e.Result);
    };

    webClient.DownloadDataAsync(URL);

    return filePath;
}