简体   繁体   中英

how to save photo in gallery folder in android using xamarin

How to save photo in gallery folder? I am using ImageCropper.Forms.Fix.v2 and Xam.Media.Plugin

code is working fine, but it saving in location: /data/user/0/com.companyname.projectname/cache/cropped3243.png

i want to save it in gallery folder. how can i do this?

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

For example,you could use DependencyService to call the natvie method to save it to the system photo album.

create a interface in your forms project:

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

then in you Android project:

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

the call it like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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