Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to save image locally

Hi there,

My application allows users to open the gallery and pick one image, now i want to pass this picked image to another view i tried to pass the source of this image but it doesn't work, so how can i save this image and use it in my code, Thanks.

Best Answers

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭
    Accepted Answer

    Hello @ImproveMe

    Here where i got the image from gallery

        private async void OpenGallery_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();
    
            var mediaOption = new PickMediaOptions
            {
                PhotoSize = PhotoSize.Custom,
            };
    
            var pick = await CrossMedia.Current.PickPhotoAsync(mediaOption);
            selectedImage.Source = ImageSource.FromStream(() => pick.GetStream());
            image = pick.Path;
         }
    

    And here where i want to pass to the next view

      private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new NavigationPage(new CropImagePage(image)));
        }
    

    Thanks.

Answers

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭
    Hello @ImproveMe I actually used this plugin and i got the image successfully from the gallery but i can't pass it to another page
  • ImproveMeImproveMe Member ✭✭✭
    edited March 2020

    Do you got errors? Passing whole Image (which will be in your RAM) to another Page could cause a problem.

    I save them locally and open them from file directory when I need them.

    you should have something like

            MediaFile file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    PhotoSize = PhotoSize.Full,
                    DefaultCamera = cameraType,
                    AllowCropping = true,
                    SaveMetaData = false,
                });
    

    then

    file.GetStream()

    and save this Stream as a png/jpg

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭
    How to save and get the image?
  • jezhjezh Member, Xamarin Team Xamurai

    We can get Path and AlbumPath:

            // Summary:
            //     Path to file
            public string Path { get; }
            //
            // Summary:
            //     Path to file
            public string AlbumPath { get; set; }
    

    When we get the file instance, then we can get path by file.Path and file.AlbumPath.

    var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Test",
                SaveToAlbum = true,
                CompressionQuality = 75,
                CustomPhotoSize = 50,
                PhotoSize = PhotoSize.MaxWidthHeight,
                MaxWidthHeight = 2000,
                DefaultCamera = CameraDevice.Front
            });
    
            if (file == null)
              return;
        System.Diagnostics.Debug.WriteLine("file AlbumPath = " + file.AlbumPath +" <----> file path= " + file.Path);
    
  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭
    @jezh Thanks for your answer, I already got the file path but when i pass this path to another view i didn't get the image, is there any way to get the image source from the image path?
  • ImproveMeImproveMe Member ✭✭✭

    Hello,

    can you post some code where you got the image and where you want to open .

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭
    Accepted Answer

    Hello @ImproveMe

    Here where i got the image from gallery

        private async void OpenGallery_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();
    
            var mediaOption = new PickMediaOptions
            {
                PhotoSize = PhotoSize.Custom,
            };
    
            var pick = await CrossMedia.Current.PickPhotoAsync(mediaOption);
            selectedImage.Source = ImageSource.FromStream(() => pick.GetStream());
            image = pick.Path;
         }
    

    And here where i want to pass to the next view

      private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new NavigationPage(new CropImagePage(image)));
        }
    

    Thanks.

  • jezhjezh Member, Xamarin Team Xamurai

    Yes, since we get the path of image,we can use function FromFile to load local images.

    For more detail about display-images, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#display-images

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭

    @ImproveMe I got this exception "System.NullReferenceException: Object reference not set to an instance of an object."
    On this line:

            ImageReadyForCrop.Source = ImageSource.FromFile(myPath);
    

    and this is myPath="/storage/emulated/0/Android/data/com.companyname.Jobs/files/Pictures/temp/images (49)_1.jpeg"

    Is the problem that the path is local?

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭

    @jezh Thanks so much but FromFile(imagePath) doesn't work

  • ImproveMeImproveMe Member ✭✭✭

    How does your CropImagePage.xaml looks like?

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭

    This is the CropImagePage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Jobs.CropImagePage">
        <ContentPage.Content>
            <StackLayout>
    
                <Image x:Name="ImageReadyForCrop">
    
                </Image>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    
  • ImproveMeImproveMe Member ✭✭✭

    One more point, you shouldn't use double Navigation

    Change from
    await Navigation.PushAsync(new NavigationPage(new CropImagePage(myPath)));
    to
    await Navigation.PushAsync(new CropImagePage(myPath));

  • Ranamuhammad.1569Ranamuhammad.1569 USMember ✭✭✭

    @ImproveMe Thank you so so much you saved my day <3

Sign In or Register to comment.