【问题标题】:How to open file on android with maui blazor app? File Chooser opens but files are disabled如何使用 maui blazor 应用程序在 android 上打开文件?文件选择器打开但文件被禁用
【发布时间】:2022-10-07 15:33:29
【问题描述】:

我使用 file-picker 指南在 maui blazor 应用程序中显示文件选择器(底部参考代码)。

在 Windows 上这有效。

在 android 上,文件选择器打开并显示,但我无法点击任何文件,因为它们都被禁用(灰色文本)并且不可点击。

我已经将[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)] 添加到MainApplication.cs<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />AndroidManifest.xml

将 apk 安装到我的设备或模拟器后,我授予了存储权限(因为我还没有实现对话框)。

如何使用 maui 应用程序在 android 设备上打开文件?


  • Visual Studio 2022(最新更新)
  • .NET 核心 6
<button @ref="button1" class="btn btn-primary" @onclick="OpenFileAsync">Open File</button>

@code {
    public async void OpenFileAsync()
    {
        var customFileType = new FilePickerFileType(
                new Dictionary<DevicePlatform, IEnumerable<string>>
                {
                    { DevicePlatform.iOS, new[] { "public.my.comic.extension" } }, // or general UTType values
                    { DevicePlatform.Android, new[] { "application/comics" } },
                    { DevicePlatform.WinUI, new[] { ".cbr", ".cbz" } },
                    { DevicePlatform.Tizen, new[] { "*/*" } },
                    { DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // or general UTType values
                });

        PickOptions options = new()
        {
            PickerTitle = "Please select a comic file",
            FileTypes = customFileType,
        };

        var result = await FilePicker.Default.PickAsync(options);
        // ... process result
    }

}

【问题讨论】:

    标签: c# android maui maui-blazor


    【解决方案1】:

    您可能希望根据您使用的平台检查接受的文件类型.. 例如在您的代码上: { DevicePlatform.Android, new[] { "application/comics" }

    接受的文件扩展名类型是“应用程序/漫画”。如果要启用图像,请更改/添加新类型到列表中。

    { DevicePlatform.Android, new[] { "application/comics","image/*" }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多