Xamarin.FilePicker返回空的FileData对象

时间:2018-07-08 22:38:32

标签: c# xamarin xamarin.forms filepicker

我正在尝试在xamarin.Forms上实现“ FilePicker”包,但不幸的是没有成功。

这是XAML.cs中的代码:

using System;
using Xamarin.Forms;
using Plugin.FilePicker;
using Plugin.FilePicker.Abstractions;

namespace FilesTest
{
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

    }

    async void Pic()
    {
        try
        {
            FileData fileData = await CrossFilePicker.Current.PickFile();
            if (fileData == null) {
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                return; // user canceled file picking
            }
            string fileName = fileData.FileName;
            string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);

            System.Console.WriteLine("File name chosen: " + fileName);
            System.Console.WriteLine("File data: " + contents);
        }
        catch (Exception ex)
        {
            System.Console.WriteLine("Exception choosing file: " + ex.ToString());
        }
    }

    private void PickFileButton_Clicked(object sender, EventArgs e)
    {
        Pic();
    }
}
}

编辑:对于那些提出要求的人,我正在尝试使其在android上运行,而我的AndroidManifest.xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.FilesTest">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
<application android:label="FilesTest.Android"></application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

(XAML中的代码只是一个简单的按钮,在单击它时会调用PickFileButton_Clicked()方法)

经过数小时的调试,除那些“〜”字符外,其他所有操作均未在控制台上写...我知道在选择文件后FileData对象为空(不,被选择的文件为简单文本文件...),但我只是找不到任何可能发生这种情况的信息。有谁知道为什么FileData对象为null?请帮忙。

1 个答案:

答案 0 :(得分:1)

它有效,谢谢所有支持者!

问题: 权限。

深入研究并学习了如何使用android和Xamarin处理权限后,我设法从手机的内存中读取了一个文本文件。

此外,我从Xam.Plugin.FilePicker切换到Xamarin.Plugin.FilePicker,该文件在Xamarin Nuget管理器上有更多下载。完全了解它是否对您有帮助。

最终代码:

using System;
using Xamarin.Forms;
using Plugin.FilePicker;
using Plugin.FilePicker.Abstractions;
using Android.Support.V4.Content;
using Android;
using Android.Support.V4.App;
using Android.App;
using Android.Content.PM;
using Android.Runtime;

namespace FilesTest
{
public partial class MainPage : ContentPage, ActivityCompat.IOnRequestPermissionsResultCallback
{
    public IntPtr Handle => default(IntPtr) ;

    public MainPage(){InitializeComponent();}

    async void Pic()//the function that triggers the file picker
    {
        try
        {
            FileData fileData = await CrossFilePicker.Current.PickFile();
            if (fileData == null) {
                return; // user canceled file picking
            }
            string fileName = fileData.FileName;
            string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);

            Console.WriteLine("File name chosen: " + fileName);
            Console.WriteLine("File data: " + contents);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception choosing file: " + ex.ToString());
        }
    }

    private void PickFileButton_Clicked(object sender, EventArgs e)//in the xaml there is a button that triggers this function
    {
        var thisActivity = Forms.Context as Activity;
        if(ContextCompat.CheckSelfPermission(thisActivity, Manifest.Permission.ReadExternalStorage) != Permission.Granted)
        {// Permission is not granted
            ActivityCompat.RequestPermissions(thisActivity, new String[] {Manifest.Permission.ReadExternalStorage }, 1);//1 is just the code to retrive this permission
        } else
        {//if permission is already granted
            Pic();
        }
    }

    public void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults) // this function is called after the permission box is shown and the user picked whether to grant or deny the permission
    {
        if(requestCode == 1)
        {
            if(grantResults.Length > 0 && grantResults[0] == Permission.Granted)
            {//permission has been granted by the user
                Pic();
            } else
            {//permission denied
            }
        }
    }

    public void Dispose(){}
}
}