我正在尝试构建一个从 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>
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;
}
}
}
以下是一些您需要注意的建议。
内容不可用。无法显示文件夹内容 因为未知错误。
这是因为模拟器上没有可显示的内容。您可以尝试在模拟器中打开照片应用程序,然后选择图像,单击左下角的共享按钮,然后选择保存到文件。保存照片后,您可以运行应用程序并从文件选择器中选择这张照片。 (确保转到浏览选项卡并从保存的路径中选择照片。)
FilePicker
,您应该启用 iCloud 功能。请添加 Entitlements.plist 文件,然后选中 iCloud
和 iCoud
文档服务。