微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 xamarin 在 android 中的画廊文件夹中保存照片

如何解决如何使用 xamarin 在 android 中的画廊文件夹中保存照片

如何将照片保存在图库文件夹中?我正在使用 ImageCropper.Forms.Fix.v2 和 Xam.Media.Plugin

代码工作正常,但保存在位置:/data/user/0/com.companyname.projectname/cache/cropped3243.png

我想把它保存在画廊文件夹中。我该怎么做?

protected async void TapGestureRecognizerCommand(object sender,EventArgs e)
        {
                await CrossMedia.Current.Initialize();

                ImageSource TempimageFile = null;
                new ImageCropper()
                {
                    PageTitle = "Crop Photo",AspectRatioX = 3,// 
                    CropShape = ImageCropper.CropShapeType.Rectangle,//Cropt shape
                    SelectSourceTitle = "Select source",// Pop up Title
                    TakePhotoTitle = "Take Photo",// Popup - 1st option 
                    PhotoLibraryTitle = "Photo Library",//Popup - 2nd option
                    Success = (imageFile) =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            TempimageFile = ImageSource.FromFile(imageFile);

                            displayAlert("test","te: " + TempimageFile,"OK");
                            ImageURL.source = TempimageFile;
                        });
                    }
                }.Show(this);
        }

解决方法

例如,您可以使用 DependencyService 调用 natvie 方法将其保存到系统相册。

在你的表单项目中创建一个界面:

public interface ISaveToGallery
{
    void Save(string filePath,string fileName);
}

然后在您的 Android 项目中:

class SaveToGallery: ISaveToGallery
{
    public void Save(string filePath,string fileName)
    {
          MediaStore.Images.Media.InsertImage(Android.App.Application.Context.ContentResolver,filePath,fileName,null);
    }
}

这样称呼:

protected async void TapGestureRecognizerCommand(object sender,EventArgs e)
    {
            await CrossMedia.Current.Initialize();

            ImageSource TempimageFile = null;
            new ImageCropper()
            {
                PageTitle = "Crop Photo",AspectRatioX = 3,// 
                CropShape = ImageCropper.CropShapeType.Rectangle,//Cropt shape
                SelectSourceTitle = "Select source",// Pop up Title
                TakePhotoTitle = "Take Photo",// Popup - 1st option 
                PhotoLibraryTitle = "Photo Library",//Popup - 2nd option
                Success = (imageFile) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        TempimageFile = ImageSource.FromFile(imageFile);

                        DisplayAlert("test","te: " + TempimageFile,"OK");
                        ImageURL.Source = TempimageFile;
                        DependencyService.Get<ISaveToGallery>().Save(imageFile,"your file name");
                    });
                }
            }.Show(this);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。