Show image from /var/mobile/Containers/Data/Application/.... path

sudhirbharti.5023sudhirbharti.5023 USMember ✭✭
edited November 2018 in Xamarin.iOS

I am trying to show the image from this location

/var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg

below is my code:

            Image img1 = new Image(); 
            img1.Source = ImageSource.FromFile("/var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg");
          //  img.Source = ImageSource.FromResource("/var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg");

I tried both options FromFile and FromResource, but nothing worked, but when I moved the image to app/resource folder or trying to access the image from the web-url it showing fine without any issue.

Any ideas how to show the image file from the local physical path (e.g. /var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg )

Thanks

Best Answers

Answers

  • JiriMatejkaJiriMatejka CZMember ✭✭✭

    How was the image placed to the location? Is this embedded within your app? Or downloaded and saved by your app? Or something else?

  • sudhirbharti.5023sudhirbharti.5023 USMember ✭✭
    edited November 2018

    @JiriMatejka said:
    How was the image placed to the location? Is this embedded within your app? Or downloaded and saved by your app? Or something else?

    Thanks for the response.

    Actually the image is saved when I clicked through the camera from my app, here is the complete code.

     async void CameraButton_Clicked(object sender, EventArgs e)
        {
            photobyte = null;
            try
            {
                var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
                {
                    Directory = "Sample",
                    //Name = strFileName,
                    Name = "sudhir_1.jpg",
                    PhotoSize = PhotoSize.Small,
                    SaveToAlbum = true
                });
    
                if (photo != null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        photo.GetStream().CopyTo(memoryStream);
                        photo.Dispose();
                        photobyte = memoryStream.ToArray();
                        var image = new Image
                        {
                            Source = ImageSource.FromStream(() => new MemoryStream(photobyte))
                        };
                        PhotoImage.Source =image.Source;
                    }
                }
            }
            catch (Exception ex)
            {
                //DisplayAlert("dd", ex.Message, "Cancel");
            }
        }
    

    Also I have verified the same image saves in Photo folder. Let me know if this helps to answer or still any query.

  • I am using the PCL. Can we use the PCL storage to get this.
  • If possible, can you share some sample code for PCL?
  • Let me try, thanks.
  • ColeXColeX Member, Xamarin Team Xamurai

    @sudhirbharti.5023 Have you solved this problem?

  • @ColeX said:
    @sudhirbharti.5023 Have you solved this problem?

    Thanks, yup.

  • ish1313ish1313 Member ✭✭

    @sudhirbharti.5023 said:
    I am trying to show the image from this location

    /var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg

    below is my code:

                Image img1 = new Image(); 
                img1.Source = ImageSource.FromFile("/var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg");
              //  img.Source = ImageSource.FromResource("/var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg");
    

    I tried both options FromFile and FromResource, but nothing worked, but when I moved the image to app/resource folder or trying to access the image from the web-url it showing fine without any issue.

    Any ideas how to show the image file from the local physical path (e.g. /var/mobile/Containers/Data/Application/B78AC09D-DFB1-4D10-9F27-07C26D2B838C/Documents/Sample/sudhir_1.jpg )

    Thanks

    just spending couple of hours on the same problem . the correct answer ( as i see it now ), that after reinstall this part of the path is changing "78AC09D-DFB1-4D10-9F27-07C26D2B838C" on IOS (on android it is stable to com../files )

    so for displaying image in iOS version i just reconstruct the path again like

                string file1 = "2.jpg";
                var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
                file1 = Path.Combine(path, file1);
       //         byte[] b = File.ReadAllBytes(file1);
    
                imgFoto.Source = ImageSource.FromFile(file1);
    
    1. pity, that Image.Source hides exception
    2. commented out string shows an exception and gave a clue

    this code should be portable and should run on both platforms, i will check it tomorrow

    just changed some code in my project and this works (on iOS at least)

        public static readonly string imgPath =  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
        .....
    
           public bool SetFotoFromArray(byte[] foto)
                {
                    DogFoodCalc.App myapp = (DogFoodCalc.App)App.Current;
                    string newfile = $"{idx}.jpg";
                    var t = myapp.db.SaveFile(newfile, foto);
                    if (t == "") return (false);
                    ImageSource = newfile;
                    return (true);
    
                }
        .....
    
            public string ImageSource  //left this for db consistency with released version
                {
                    get => imgsource;
                    set
                    {
                            imgsource = value;
                            BindingImageSource = null;
                            OnPropertyChanged();
                    }
                }
    
                [Ignore]
                public string BindingImageSource
                {
                    get => Path.Combine(imgPath, imgsource);
                    set => OnPropertyChanged(); 
                }
    
        in xaml 
    
          <Image Source="{Binding BindingImageSource}"  x:Name="imgFoto" Style="{StaticResource PetImageStyle}" 
                               WidthRequest= "150" HeightRequest="150" />
    

    hope this helps someone and save some time :)

    ish

Sign In or Register to comment.