Unknown Error for File Picker in IOS Simulator

ChompPowerPlatform 20 Reputation points
2023-08-12T12:18:34.9333333+00:00

I am trying to build an app which uploads and retrieves images from a Firebase Storage bucket.

The issue is when I try to upload a file it says in the file picker in the IOS simulator the following error: Content Unavailable. The folder contents could not be displayed becuase of an unknown error. Check also the image below.

I have even downloaded the images and made sure they are in the Photos.

What I have also tried is updating the Info.plist with the following:

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

Image of the error

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;
        }
    }
}
Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-08-14T08:58:36.6466667+00:00

    Hello,

    I have even downloaded the images and made sure they are in the Photos.

    If you want to select an image in Photos, you should replace the file picker with media picker.

    You got the "Content Unavailable" error because there is no content to display on the simulator. You could try opening the "Photos" app in the simulator, then selecting an image, clicking the left-bottom share button, and choosing "Save to Files". After saving the photo, you can run your app and choose this photo from the file picker. (Pay attention to going to "Browse" tab and selecting the photo from the saved path.)

    In addition, to access the FilePicker, you should enable iCloud capabilities. Please add an Entitlements.plist file, then check iCloud and iCoud Documents service.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.