Xamarin.Forms, How to open default gallery app

Hello and thanks for reading.

I'm doing a issues ticket app and users are able to upload a photo and look at the photo. But I want to tap on the photo and open it with the default gallery app in the device (IOS/Android).

I can't find any reference on documentation nor google search, this is my last resort.

Image data (/folder/file.jpg) is stored in SQLlite locally, image is on a folder on server.

I have this part of code:

if (Fotos.Count > 0)
{
    DatoParteBottom.Children.Add(new BoxView() { BackgroundColor = color, HeightRequest = 1, WidthRequest = 1 });
    DatoParteBottom.Children.Add(new Label { HorizontalOptions = LayoutOptions.CenterAndExpand, Text = "Fotos", FontSize = 14 });
    DatoParteBottom.Children.Add(new BoxView() { BackgroundColor = color, HeightRequest = 1, WidthRequest = 1 });
    foreach (var foto in Fotos)
    {
        DatoParteBottom.Children.Add(new Image { Source = url + foto.ID_Parte + "/" + foto.Archivo, });
    }
}

EDIT:

I would accept any plugin to solve this.

EDIT...

I got 2 answers telling me something i didn't ask for.

I just want to make an image already in the APP opened by the default gallery app from the device.

I don't want to open the gallery to choose another image from device to change the photo from the App.

BIG EDIT:

So @Adrain Zhu -MSFT had an idea: Told me to use

await Launcher.OpenAsync(new OpenFileRequest { File=new ReadOnlyFile(photo.FullPath)});

And I tried. photo.FullPath can't be an URL, and all my images were online, so I had to download the image first. I added a code I found and I don't know if it works very well: Code:

public string DownloadImageString(string URL)
{
    WebClient webClient = new WebClient();

    string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), 
   "Images", "temp");
    string fileName = URL.ToString().Split('/').Last();
    string filePath = Path.Combine(folderPath, fileName);

    webClient.DownloadDataCompleted += (s, e) =>
    {
        Directory.CreateDirectory(folderPath);

        File.WriteAllBytes(filePath, e.Result);
    };

    webClient.DownloadDataAsync(new Uri(URL));

    return filePath;
}

So now I can give the function of Adrain Zhu -MSFT a real Path. The Path is:

/data/user/0/com.companyname.workersapp/files/Images/temp/2021062311191329243400.jpg

It says that the file cannot be found in that path. Do I need permissions or something in the manifest?

-Url of image is HTTPS.

This is how I tap the image:


Image imagen = new Image
{
    Source = url + Foto.ID_Parte + "/" + Foto.Archivo
};
var imagen_tap = new TapGestureRecognizer();
imagen_tap.Tapped += async (s, e) =>
{
    string path = api.DownloadImageString(url + Foto.ID_Parte + "/" + Foto.Archivo);
    await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(path) });
};
                
imagen.GestureRecognizers.Add(imagen_tap);



Fixed:

I fixed the problem of above easily because string path was returning /data/images/temp[file]
I added a +"/" and it fixed the problem.

string filePath = Path.Combine(folderPath + "/", fileName);

Upvotes: 1

Views: 2162

Answers (2)

Adrain
Adrain

Reputation: 1934

1.You can try to work with Launcher.OpenAsync() method, code like:

await Launcher.OpenAsync(new OpenFileRequest { File=new ReadOnlyFile(photo.FullPath)});
  1. Otherwise, you can implement it using dependency service on each platform. Here is a link about how to implement on IOS iOS - open photo from photo album in my app

Upvotes: 2

jose_m
jose_m

Reputation: 191

Xamarin.Essentials with MediaPicker class could help:

https://learn.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android

You need PickPhotoAsync(MediaPickerOptions)method:

https://learn.microsoft.com/en-us/dotnet/api/xamarin.essentials.mediapicker?view=xamarin-essentials

You also can use Stormlion Photo Browser to present a photo:

https://github.com/stormlion227/PhotoBrowser.Forms

Upvotes: 0

Related Questions