1

I've been trying to invoke the native Hololens file picker via a button on a unity app (although I don't really know if it has one). I'm using MRTK in unity to build the app, and this is the code I'm currently using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

#if ENABLE_WINMD_SUPPORT
using Windows.Storage;
using Windows.Storage.Streams;
#endif

public class OpenFilePicker : MonoBehaviour
{
    public void OpenFile()
    {

#if ENABLE_WINMD_SUPPORT
        FilePicker();
#endif
    }

    private async void FilePicker()
    {
#if ENABLE_WINMD_SUPPORT
        var picker = new Windows.Storage.Pickers.FileOpenPicker();
        picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
        picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".jpeg");
        picker.FileTypeFilter.Add(".png");
        
        await picker.PickSingleFileAsync();
#endif
    }
}

Upon clicking the button, OpenFile() is invoked. The code compiles and builds with no errors but fails to produce any result on the Hololens 2 emulator. I've been scouring the internet for information on how this is done but all I've found is this and a bunch of outdated posts.

mobius
  • 11
  • 1
  • Does this answer your question? [System.Exception getting called when assessing OpenFilePicker on Hololens](https://stackoverflow.com/questions/66281959/system-exception-getting-called-when-assessing-openfilepicker-on-hololens/66282480#66282480) – Hernando - MSFT Aug 11 '21 at 06:19

1 Answers1

0

Try this, I had a very similar problem where my code built fine but nothing happened when I pressed the button. I had to change the type of UWP check.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if WINDOWS_UWP
using System;
using Windows.Storage;
using Windows.System;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Storage.Pickers;
#endif

public class OpenFilePicker : MonoBehaviour
{

    public void OpenFile()
    {
        FilePicker();
    }

    private void FilePicker()
    {
#if !UNITY_EDITOR && UNITY_WSA_10_0

        UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
            var filepicker = new FileOpenPicker();
            filepicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filepicker.FileTypeFilter.Add(".jpeg");
            filepicker.FileTypeFilter.Add(".jpg");
            filepicker.FileTypeFilter.Add(".png");

            var file = await filepicker.PickSingleFileAsync();
            UnityEngine.WSA.Application.InvokeOnAppThread(() => 
            {
                // do something with file

            }, false);
        }, false);

#endif
    }
}

Greta-A
  • 43
  • 5