【问题标题】:how to save photo in gallery folder in android using xamarin如何使用 xamarin 将照片保存在 android 的画廊文件夹中
【发布时间】:2021-05-10 14:55:23
【问题描述】:

如何将照片保存在图库文件夹中?我正在使用 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);
        }

【问题讨论】:

  • 插件中似乎没有执行此操作的选项
  • Android 设备上没有图库文件夹
  • @jason,我只能使用 Xam.Media.Plugin 保存在图库中,但它没有裁剪选项... ImageCropper.Forms.Fix.v2 有裁剪选项但没有保存到图库跨度>
  • 知道我该怎么做吗?也许不同的插件?任何参考链接都会有所帮助
  • 它是一个开源插件,修改它来做你想做的并提交一个PR

标签: c# android xamarin xamarin.forms xamarin.android


【解决方案1】:

例如,您可以使用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);
    }

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2012-09-12
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多