View a specific image from the complete gallery path without browsing

126 views Asked by At

I'm trying:

   <Image Source="/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg"/>

No effect.
File imageSpc = new File('/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg');
var imageSpc = new File('/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg');
Error, cannot create static variable for type File or var.

            <Image x:Name="fotka" Aspect="AspectFit" HeightRequest = "50"
    WidthRequest = "60"/>

fotka.Source = ImageSource.FromFile("/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg");
No effect
Is possible to show specific image from gallery in XAML or at least from the code

enter image description here

I did enable ADB notification option in device. In The moment image to be displayed compiler show error:

enter image description here

How to fix this Error?

2

There are 2 answers

9
Cherry Bu - MSFT On

According to your description, you want to display image using gallery path, if yes, please take a look the following code:

Firstly, you need to get image path from gallery in Android or ios.

Create interface IFileSystem in PCL(Forms)

public interface IFileSystem
{       
     string GetGalleryImage();
}

Then implementing this interface in platform, the example is in Android.

[assembly: Dependency(typeof(FileSystemImplementation))]
namespace demo3.Droid
{
public class FileSystemImplementation : IFileSystem
{

    public string GetGalleryImage()
    {      
        var filePath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
        var path = System.IO.Path.Combine(filePath.AbsolutePath, "Test/image1.jpg");
        return path;
    }
}
}

Add one Image control in PCL(Forms), name image1, using DependencyService to get image path.

  <Button
            x:Name="btn1"
            Clicked="Btn1_Clicked"
            Text="load image" />
        <Image
            x:Name="image1"
            HeightRequest="100"
            WidthRequest="100" />

  private void Btn1_Clicked(object sender, EventArgs e)
    {         
        var path = DependencyService.Get<IFileSystem>().GetGalleryImage();
        image1.Source = ImageSource.FromFile(path);
    }

This is article about DependencyService, you can take a look:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

Update:

enter image description here

enter image description here

0
Tamtee On

I found, each image before show in activity must be resized to max size 4096 x 4096 px otherwise image not shown, without any errors message.