728x90
반응형
    private async void OnButtonClicked(object sender, EventArgs e)
    {
        // 확장자 기준 파일만
        PickOptions option = new PickOptions();
        option.PickerTitle = "Select File";
        option.FileTypes = FilePickerFileType.Pdf;
        var fileResult1 = await PickAndShow(option);

        // 이미지 파일만
        var fileResult = await PickAndShow(PickOptions.Images);

        if (fileResult != null)
        {
            var stream = await fileResult.OpenReadAsync();
            image.Source = ImageSource.FromStream(() => { return stream; });
        }
    }

    public async Task<FileResult> PickAndShow(PickOptions options)
    {
        try
        {
            var result = await FilePicker.Default.PickAsync(options);
            if (result != null)
            {
                if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                    result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                {
                    using var stream = await result.OpenReadAsync();
                    var image = ImageSource.FromStream(() => stream);
                }
            }

            return result;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }

        return null;
    }
728x90
반응형
Posted by kjun

댓글을 달아 주세요