How can i store image from my cache memory into device external memory & later save it on webServer?

In this example, I am using ImageCropper.Forms Nugets Package.

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
CrossMedia.Current.Initialize();
}
protected void OnClickedRectangle(object sender, EventArgs e)
{
new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle,
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
imageView.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
}
private void OnClickedCircle(object sender, EventArgs e)
{

        new ImageCropper()
        {
            CropShape = ImageCropper.CropShapeType.Oval,

            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    imageView.Source = ImageSource.FromFile(imageFile);
                 //In imageFile : i get the this location in
                  // imageFile => "/data/user/0/com.companyname.ImageCropperExample1/cache/cropped-1919527172.jpg"
                });
            }
        }.Show(this);
    }
  }

Posts

  • I have found the solution..

    I add the new class in PCL named IFileReadWrite

    public interface IFileReadWrite
    {
    void WriteData(string filepath);
    string ReadData(string filename);
    }

    after that i add the class in Droid(Android) Project FileHelper

    using ImageCropperExample1.Droid;
    using Xamarin.Forms;

    [assembly: Dependency(typeof(FileHelper))]
    namespace ImageCropperExample1.Droid
    {
    public class FileHelper : IFileReadWrite
    {
    public string ReadData(string filename)
    {
    var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    var filePath = Path.Combine(documentsPath, filename);
    return File.ReadAllText(filePath);
    }

        public void WriteData(string filepath)
        {
            var imageData = System.IO.File.ReadAllBytes(filepath);
            var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(
            Android.OS.Environment.DirectoryDcim);
            var pictures = dir.AbsolutePath;
            var filename = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
            var newFilepath = System.IO.Path.Combine(pictures, filename);
    
            System.IO.File.WriteAllBytes(newFilepath, imageData);
            //mediascan adds the saved image into the gallery
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(newFilepath)));
            Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent);
    
    
          }
        }
    }
    

    then changed in the MainPage.xaml.cs file

    public partial class MainPage : ContentPage
    {

        public MainPage()
        {
            InitializeComponent();
            CrossMedia.Current.Initialize();
        }
        protected void OnClickedRectangle(object sender, EventArgs e)
        {
            new ImageCropper()
            {
                            PageTitle = "Test Title",
                AspectRatioX = 1,
                AspectRatioY = 1,
                CropShape = ImageCropper.CropShapeType.Oval,
                Success = (imageFile) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        imageView.Source = ImageSource.FromFile(imageFile);
                    });
                }
            }.Show(this);
        }
        private void OnClickedCircle(object sender, EventArgs e)
        {
            new ImageCropper()
            {
                CropShape = ImageCropper.CropShapeType.Oval,
               Success = (imageFile) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        imageView.Source = ImageSource.FromFile(imageFile);
    
    
                        DependencyService.Get<IFileReadWrite>().WriteData(imageFile);
                    });
                }
            }.Show(this);
        }       
    }
    
Sign In or Register to comment.