将照片从图库共享到我的 MAUI .NET Android 应用程序后如何获取照片路径

问题描述 投票:0回答:1

我想将照片分享到我的 MAUI .NET Android 应用程序。我目前唯一得到的是照片 URI。

这是我的 MainActivity.cs:

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, LaunchMode = LaunchMode.SingleTask)]
[IntentFilter(new[] { "android.intent.action.SEND" }, Categories = new[] { "android.intent.category.DEFAULT" }, DataMimeType = "*/*")]
[IntentFilter(new[] { "android.intent.action.SEND_MULTIPLE" }, Categories = new[] { "android.intent.category.DEFAULT" }, DataMimeType = "*/*")]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnNewIntent(Intent? intent)
    {
        base.OnNewIntent(intent);
        if (intent.Type == "image/jpeg")
        {
            var data = intent?.ClipData?.GetItemAt(0);
        }
    }
}

如何获取照片路径,并将其显示在我的应用程序中?

android android-activity maui
1个回答
0
投票

通常,当您将照片共享到 MAUI.NET Android 应用程序并接收 URI 时,您会处理内容 URI (content://),而不是直接文件路径。

以下是如何从 URI 获取图像并将其显示在 MAUI 应用程序中:

  1. 访问 URI:正如您所确定的,共享意图提供了照片的 URI。使用此 URI 访问照片。

  2. 读取输入流:使用内容解析器打开内容 URI 的输入流。

  3. 转换为可显示格式:由于您无法直接使用作用域存储中的文件路径,因此请将输入流转换为您的应用程序可以显示的格式,例如 StreamImageSource 或将其临时保存到应用程序的私有存储中。

第 1 步:设置 MainActivity.cs

using Android.App;
using Android.Content;
using Android.Content.PM;
using Microsoft.Maui;
using System;
using System.IO;

namespace YourNamespace
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, LaunchMode = LaunchMode.SingleTask)]
    [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "image/*")]
    public class MainActivity : MauiAppCompatActivity
    {
        protected override void OnNewIntent(Intent? intent)
        {
            base.OnNewIntent(intent);
            HandleIntent(intent);
        }

        private void HandleIntent(Intent? intent)
        {
            if (intent?.Type?.StartsWith("image/") == true)
            {
                // Assuming single image share
                Android.Net.Uri imageUri = intent.Data ?? intent.ClipData?.GetItemAt(0)?.Uri;
                if (imageUri != null)
                {
                    try
                    {
                        // Get InputStream from the Uri
                        using var inputStream = ContentResolver.OpenInputStream(imageUri);
                        
                        // Convert InputStream to a MemoryStream for easier handling
                        var memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                        memoryStream.Position = 0;
                        
                        DisplayImage(memoryStream);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error accessing shared image: {ex.Message}");
                    }
                }
            }
        }

        private void DisplayImage(MemoryStream memoryStream)
        {
            var imageSource = ImageSource.FromStream(() => new 
            MemoryStream(memoryStream.ToArray()));

            if (App.MainPageInstance != null)
            {
               MainThread.BeginInvokeOnMainThread(() =>
               {
                  App.MainPageInstance.UpdateImage(imageSource);
               });
            }
        }

    }
}

第 2 步:在应用程序中显示图像

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourNamespace.MainPage">

    <VerticalStackLayout Spacing="25" Padding="30">
        <Image x:Name="SharedImage" />
    </VerticalStackLayout>

</ContentPage>

并在 MainPage.xaml.cs 中添加更新图像的方法:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    public void UpdateImage(ImageSource imageSource)
    {
        SharedImage.Source = imageSource;
    }
}

第3步:公开MainPage实例

public partial class App : Application
{
    public static MainPage MainPageInstance { get; private set; }

    public App()
    {
        InitializeComponent();

        MainPage mainPage = new MainPage();
        MainPage = mainPage;
        MainPageInstance = mainPage;
    }
}

Ο显然,代码的结构并不遵循任何像MVVM那样的模式,你可以修改它以遵循它们并改进代码。

© www.soinside.com 2019 - 2024. All rights reserved.