xamarin 无法从Xmarin.Essentials.FilePicker中选择文件,所有文件都被禁用

9njqaruj  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(339)

When try to pick up the file all files are showing as disable. I am unable select the files. I am using below code.

var options = new PickOptions
                        {
                            PickerTitle = "Please select a pdf file",
                            FileTypes = FilePickerFileType.Pdf
                        };
                    var fileResult = await FilePicker.PickAsync(options);
                    var file = await Constants.ValidateFileNew(fileResult);
                    if (file.IsValid )
                    {
                        var fileName = file.FileName;
                        var data = file.fileData;
                        Stream stream = new MemoryStream(data);
                        fileID += 1;
                        ListNursingAssessmentFileNames.Add(new FileEntity { FileName = fileName, FileID = fileID, File = data });
                        DependencyService.Get<IJsonFileService>().SaveToLibrary<ObservableCollection<FileEntity>>(ListNursingAssessmentFileNames, "NursingInitialAssessmentFiles.json");
                        stackDigitalForm.IsVisible = false;
                    }

I have provide exteral storage permission. Storage Permission SS

tjrkku2a

tjrkku2a1#

You need specify custom files types when creating the PickOptions and they can be customized per platform.
Per docs , PickOptions.FileTypes has many types in each platform,
On Android and iOS the files not matching this list is only displayed grayed out. When the array is null or empty, all file types can be selected while picking. The contents of this array is platform specific; every platform has its own way to specify the file types. On Android you can specify one or more MIME types, e.g. "image/png"; also wild card characters can be used, e.g. "image/*". On iOS you can specify UTType constants, e.g. UTType.Image. On UWP, specify a list of extensions, like this: ".jpg", ".png".
You can refer to the following sample code:
I add a button to trigger the selection event,

private void PickFileButton_Clicked(object sender, EventArgs e)
{
    Task<FileResult> results = PickAndShow();
}
//for custom types
async Task<FileResult> PickAndShow()
{
    var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
    { 
        { DevicePlatform.iOS, new[] { "com.adobe.pdf", "UTType.Image" } }, // or general UTType values
        { DevicePlatform.Android, new[] { "application/pdf", "image/*" } },
        { DevicePlatform.UWP, new[] { ".pdf", ".jpg", ".png" } },
        { DevicePlatform.Tizen, new[] { "*/*" , ".png" } },
        { DevicePlatform.macOS, new[] { "pdf" , "public.image" } },
    });
    
    try
    {
        var result = await FilePicker.PickAsync(new PickOptions
        {
            PickerTitle = "Please select files",
            FileTypes = customFileType,
        });
        if (result != null)
        {
            FileName.Text = $"File Name: {result.FileName}";
            if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
            {
                var stream = await result.OpenReadAsync();
                FileImage.Source = ImageSource.FromStream(() => stream);
            }
        }
        return result;
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }
    return null;
}

Note:Don't forget to add WRITE_EXTERNAL_STORAGE and RAED_EXTERNAL_STORAGE in your manifest file.
Official reference docs: https://learn.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fxamarin-forms&tabs=android

相关问题