繁体   English   中英

如何使用 xamarin 将照片保存在 android 的图库文件夹中

[英]how to save photo in gallery folder in android using xamarin

如何将照片保存在图库文件夹中? 我正在使用 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 方法将其保存到系统相册中。

在 forms 项目中创建一个接口:

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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM