简体   繁体   English

Xamarin.Forms,如何打开默认图库应用

[英]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).但我想点击照片并使用设备(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.图像数据 (/folder/file.jpg) 存储在本地 SQLlite 中,图像位于服务器上的文件夹中。

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.我得到了 2 个答案,告诉我一些我没有要求的事情。

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所以@Adrain Zhu -MSFT 有一个想法:告诉我使用

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. photo.FullPath 不能是网址,而且我所有的图片都是在线的,所以我必须先下载图片。 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.所以现在我可以给 Adrain Zhu -MSFT 的函数一个真正的 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. - 图像的 URL 是 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); string filePath = Path.Combine(folderPath + "/", fileName);

Xamarin.Essentials with MediaPicker class could help:带有 MediaPicker 类的 Xamarin.Essentials 可以帮助:

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

You need PickPhotoAsync(MediaPickerOptions)method:您需要 PickPhotoAsync(MediaPickerOptions) 方法:

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

You also can use Stormlion Photo Browser to present a photo:您还可以使用 Stormlion 照片浏览器来展示照片:

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

1.You can try to work with Launcher.OpenAsync() method, code like: 1.您可以尝试使用 Launcher.OpenAsync() 方法,代码如下:

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这是一个关于如何在 IOS iOS上实现的链接- 在我的应用程序中打开相册中的照片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM