Xamarin.Forms,如何打开默认图库应用 [英] Xamarin.Forms, How to open default gallery app

查看:32
本文介绍了Xamarin.Forms,如何打开默认图库应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢阅读.

我正在开发一个问题工单应用,用户可以上传照片并查看照片.但我想点击照片并使用设备(IOS/Android)中的默认图库应用打开它.

我找不到任何关于文档或谷歌搜索的参考,这是我最后的手段.

图像数据(/folder/file.jpg)存储在本地 SQLlite 中,图像位于服务器上的文件夹中.

我有这部分代码:

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

我会接受任何插件来解决这个问题.

编辑...

我得到了 2 个答案,告诉我一些我没有要求的东西.

我只想在设备的默认图库应用程序打开的应用程序中制作一个图像.

我不想打开图库从设备中选择另一张图片来更改应用中的照片.

所以@Adrain Zhu -MSFT 有了一个想法:告诉我用

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

我试过了.photo.FullPath 不能是网址,而且我所有的图片都是在线的,所以我必须先下载图片.我添加了一个我找到的代码,我不知道它是否工作得很好:代码:

public string DownloadImageString(string URL){WebClient webClient = new WebClient();字符串 folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),图像"、温度");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));返回文件路径;}

所以现在我可以给 Adrain Zhu -MSFT 的函数一个真正的 Path.路径是:

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

它说在该路径中找不到该文件.我需要权限或清单中的某些内容吗?

-图片的 URL 是 HTTPS.

这是我点击图像的方式:

<预><代码>图像 imagen = 新图像{来源 = url + Foto.ID_Parte + "/";+ Foto.Archivo};var imagen_tap = new TapGestureRecognizer();imagen_tap.Tapped += async (s, e) =>{字符串路径 = api.DownloadImageString(url + Foto.ID_Parte + "/" + Foto.Archivo);await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(path) });};imagen.GestureRecognizers.Add(imagen_tap);固定的:我很容易解决了上面的问题,因为字符串路径正在返回/data/images/temp[file]我添加了一个 +/"它解决了这个问题.

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

解决方案

1.您可以尝试使用 Launcher.OpenAsync() 方法,代码如下:

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

  1. 否则,您可以在每个平台上使用依赖服务来实现它.这是一个关于如何在 IOS 上实施的链接 iOS - 打开照片来自我应用中的相册

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);

解决方案

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

这篇关于Xamarin.Forms,如何打开默认图库应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆