Image Xamarin表单将图像从url保存到设备';s画廊

Image Xamarin表单将图像从url保存到设备';s画廊,image,xamarin,xamarin.forms,xamarin.android,xamarin.ios,Image,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我正在使用Xamarin表单(使用iOS和Android)。我想做的是允许用户使用DependencyService从url下载图像。我尝试在IOS emulator中运行,图像确实保存到了emulator中,但没有显示在图库中。 感谢您的帮助,以下是我的代码: 在ViewModel中: public void DownloadImage() { IFileService fileSvc = DependencyService.Get<

我正在使用Xamarin表单(使用iOS和Android)。我想做的是允许用户使用DependencyService从url下载图像。我尝试在IOS emulator中运行,图像确实保存到了emulator中,但没有显示在图库中。 感谢您的帮助,以下是我的代码:

在ViewModel中:

        public void DownloadImage()
        {
            IFileService fileSvc = DependencyService.Get<IFileService>();

            WebClient wc = new WebClient();
            byte[] bytes = wc.DownloadData(ImgUrl);
            Stream stream = new MemoryStream(bytes);

            fileSvc.SavePicture(DateTime.Now.ToString(), stream, "temp");

        }
在Xamarin,机器人

        public void SavePicture(string name, Stream data, string location = "temp")
        {
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            documentsPath = Path.Combine(documentsPath, "Images", 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);
            }
        }

如果要将图像保存到Gallery中,请按照以下代码操作

首先,在PCL中创建IMediaService接口

  public interface IMediaService
{
    void SaveImageFromByte(byte[] imageByte,string filename);
}
然后在特定平台中实现该接口 Android项目

 public  class MediaService : IMediaService
{
    Context CurrentContext => CrossCurrentActivity.Current.Activity;
    public void SaveImageFromByte(byte[] imageByte, string filename)
    {
        try
        {
            Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
            string path = System.IO.Path.Combine(storagePath.ToString(), filename);
            System.IO.File.WriteAllBytes(path, imageByte);
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
            CurrentContext.SendBroadcast(mediaScanIntent);
        }
        catch (Exception ex)
        {

        }
    }
}
在特定平台中实现此接口 Xamarin.iOS项目

public class MediaService : IMediaService
{
    public void SaveImageFromByte(byte[] imageByte,string fileName)
    {
        var imageData = new UIImage(NSData.FromArray(imageByte));
        imageData.SaveToPhotosAlbum((image, error) =>
        {
            //you can retrieve the saved UI Image as well if needed using  
            //var i = image as UIImage;  
            if (error != null)
            {
                Console.WriteLine(error.ToString());
            }
        });
    }
}

要访问CurrentContext,请从NuGet软件包管理器安装NuGet软件包(Plugin.CurrentActivity),同时检查外部存储权限

如果要将图像保存到Gallery中,请按照以下代码操作

首先,在PCL中创建IMediaService接口

  public interface IMediaService
{
    void SaveImageFromByte(byte[] imageByte,string filename);
}
然后在特定平台中实现该接口 Android项目

 public  class MediaService : IMediaService
{
    Context CurrentContext => CrossCurrentActivity.Current.Activity;
    public void SaveImageFromByte(byte[] imageByte, string filename)
    {
        try
        {
            Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
            string path = System.IO.Path.Combine(storagePath.ToString(), filename);
            System.IO.File.WriteAllBytes(path, imageByte);
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
            CurrentContext.SendBroadcast(mediaScanIntent);
        }
        catch (Exception ex)
        {

        }
    }
}
在特定平台中实现此接口 Xamarin.iOS项目

public class MediaService : IMediaService
{
    public void SaveImageFromByte(byte[] imageByte,string fileName)
    {
        var imageData = new UIImage(NSData.FromArray(imageByte));
        imageData.SaveToPhotosAlbum((image, error) =>
        {
            //you can retrieve the saved UI Image as well if needed using  
            //var i = image as UIImage;  
            if (error != null)
            {
                Console.WriteLine(error.ToString());
            }
        });
    }
}

要访问CurrentContext,请从NuGet软件包管理器安装NuGet软件包(Plugin.CurrentActivity),同时检查外部存储权限

,谢谢您的回复。当我尝试在ios emulator上运行时,一旦调试器触及`imageData.SaveToPhotosAlbum((图像,错误),应用程序强制退出而不生成任何错误=>`你知道问题出在哪里吗?@LongHaoLi在iOS上当你想把照片写到相册时,我们需要在info.plist中添加隐私。在iOS 11上添加NSPhotoLibraryAddUsageDescription,iOS 10上添加NSPhotoLibraryUsageDescription。这里有一些关于和的文章,嘿@CherryBu,非常感谢你的帮助!在我添加之后它就起作用了NSPhotoLibraryAddUsageDescription进入info.plist!嘿,Cherry,谢谢你的回复。当我尝试在ios emulator上运行时,一旦调试器触及'imageData.SaveToPhotosAlbum((图像,错误),应用程序强制退出而不产生任何错误=>`你知道问题出在哪里吗?@LongHaoLi在iOS上当你想把照片写到相册时,我们需要在info.plist中添加隐私。在iOS 11上添加NSPhotoLibraryAddUsageDescription,iOS 10上添加NSPhotoLibraryUsageDescription。这里有一些关于和的文章,嘿@CherryBu,非常感谢你的帮助!在我添加之后它就起作用了NSPhotoLibraryAddUsageDescription到info.plist!