在 Xamarin Forms 中将图像从图像框中保存到图库 [英] Save image from an image box to gallery in Xamarin Forms

查看:49
本文介绍了在 Xamarin Forms 中将图像从图像框中保存到图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将图像控件中的图片保存到 Xamarin Forms 中的 Android 库?感谢所有帮助.

Is there a way to save the picture in an image control to the Android gallery in Xamarin Forms? All help is appreciated.

var image = new Image();
image.Source = "test.png";

截图

推荐答案

您可以使用 Dependency ServiceResource/drawable 图像中获取流.

You could use Dependency Service to get the stream from Resource/drawable image.

创建 IDependency 接口.

Create the IDependency interface.

public interface IDependency
{
    MemoryStream DrawableByNameToByteArray(string fileName);
}

安卓实现

public class DependencyImplementation : IDependency
{
    public MemoryStream DrawableByNameToByteArray(string fileName)
    {
        var context = Application.Context;
        using (var drawable = Xamarin.Forms.Platform.Android.ResourceManager.GetDrawable(context, fileName))
        using (var bitmap = ((BitmapDrawable)drawable).Bitmap)
        {
            var stream = new MemoryStream();
            bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
            bitmap.Recycle();
            return stream;
        }
    }
}

IOS 的实现可以参考 SO 中的线程.将图像(从 drawable 文件夹)转换为 ByteArray

For the IOS implementation, you could refer to the thread in SO. Convert Image (from drawable folder) to ByteArray

然后在 Android Mainactivity 中注册.

And then register in Android Mainactivity.

DependencyService.Register<IDependency, DependencyImplementation>();

如果您的android版本高于android 6.0,则此问题需要运行时存储权限.请使用运行时权限检查 Plugin.Permissions.https://github.com/jamesmontemagno/PermissionsPlugin

If your android version is highter than android 6.0, you need runtime permission for storage in this question. Please check the Plugin.Permissions with runtime permission. https://github.com/jamesmontemagno/PermissionsPlugin

之后,您可以将图像保存到图片内部存储中.

After that, you could save the image to picture internal storage.

var filename = "";
        var source = image.Source as FileImageSource;
        if (source != null)
        {
            filename = source.File;
        }

        var savingFile = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
        //var savingFile1 = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "save.png");
        var S = DependencyService.Get<IDependency>().DrawableByNameToByteArray(filename);


        if (!File.Exists(savingFile))
        {
            File.WriteAllBytes(savingFile, S.ToArray());               
        }

在内部存储中,没有 root 权限您无法查看文件.如果你想查看它,你可以使用 adb 工具.请检查链接中的方式.如何在登录成功时将用户名写入本地txt文件并在下次登录时检查文件?

In Internal Storage, you couldn't see the files without root permission. If you want to view it, you could use adb tool. Please check the way in link. How to write the username in a local txt file when login success and check on file for next login?

这篇关于在 Xamarin Forms 中将图像从图像框中保存到图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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