IOS 模拟器中文件选择器出现未知错误

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

我正在尝试构建一个从 Firebase 存储桶上传和检索图像的应用程序。

问题是当我尝试上传文件时,它在 IOS 模拟器的文件选择器中显示以下错误:

Content Unavailable. The folder contents could not be displayed becuase of an unknown error.
另请检查下图。

我什至下载了图像并确保它们在照片中。

我还尝试过使用以下内容更新 Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>Your reason for accessing the photo library</string>
<key>NSMicrophoneUsageDescription</key>
<string>Your reason for accessing the microphone</string>
<key>NSCameraUsageDescription</key>
<string>Your reason for accessing the camera</string>

Error Description

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="fbStorage.MainPage">

    <StackLayout>
        <Button Text="Pick and Upload" Clicked="OnUploadButtonClicked" />
        <Image x:Name="displayImage" />
    </StackLayout>

</ContentPage>

MainPage.xaml.cs:

using Firebase.Storage;

namespace fbStorage;

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

    //File Picker below
    async void OnUploadButtonClicked(object sender, EventArgs e)
    {
        try
        {
            FileResult result = await FilePicker.PickAsync();
            if (result != null)
            {
                Stream stream = await result.OpenReadAsync();
                await UploadFileToFirebaseStorage(stream, result.FileName);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    async void DisplayImageFromFirebase(string filename)
    {
        try
        {
            Stream stream = await GetFileFromFirebaseStorage(filename);
            displayImage.Source = ImageSource.FromStream(() => stream);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }

    async Task UploadFileToFirebaseStorage(Stream stream, string filename)
    {
        var storage = new FirebaseStorage("link");

        var imageRef = storage.Child("images").Child(filename);

        var task = imageRef.PutAsync(stream);
        task.Progress.ProgressChanged += (s, e) =>
        {
        };

        await task;

        DisplayImageFromFirebase(filename);
    }

    async Task<Stream> GetFileFromFirebaseStorage(string filename)
    {
        var storage = new FirebaseStorage("link");

        var imageRef = storage.Child("images").Child(filename);
        
        var downloadUrl = await imageRef.GetDownloadUrlAsync();
        
        using (var httpClient = new HttpClient())
        {
            var stream = await httpClient.GetStreamAsync(downloadUrl);
            return stream;
        }
    }
}

c# firebase firebase-storage maui
1个回答
0
投票

以下是一些您需要注意的建议。

  1. 如果您想在照片中选择图像,则应将文件选择器替换为照片和视频媒体选择器

  2. 根据以下错误消息,

内容不可用。无法显示文件夹内容 因为未知错误。

这是因为模拟器上没有可显示的内容。您可以尝试在模拟器中打开照片应用程序,然后选择图像,单击左下角的共享按钮,然后选择保存到文件。保存照片后,您可以运行应用程序并从文件选择器中选择这张照片。 (确保转到浏览选项卡并从保存的路径中选择照片。)

  1. 要访问
    FilePicker
    ,您应该启用 iCloud 功能。请添加 Entitlements.plist 文件,然后选中
    iCloud
    iCoud
    文档服务。
© www.soinside.com 2019 - 2024. All rights reserved.