用Xamarin读取一个android文件

时间:2016-07-25 19:02:03

标签: xamarin xamarin.android

这可能非常简单但我无法从文件中读取字节。我在iOS上工作得很好但是在Android上它始终与DirectoryNotFoundException崩溃。所以我认为uri错了,但我不确定我错在哪里......

我的视图有一个按钮,可以打开文件选择器......

var selectedDocumentPath = await DependencyService.Get<IFilePicker>().pickDocument();

我的FilePicker类......

class FilePicker : IFilePicker
{
    public async Task<string> pickDocument()
    {
        string fileUri = string.Empty; 
        Console.WriteLine("Selecting Document from android device.");
        Intent intent = new Intent(Intent.ActionOpenDocument);
        intent.SetType("*/*");
        intent.PutExtra(Intent.ExtraMimeTypes, new string[] { "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
        intent.AddCategory(Intent.CategoryOpenable);

        ((Activity)Forms.Context).StartActivityForResult(Intent.CreateChooser(intent, "Select Document"), MainActivity.DOCUMENT_CODE);

        return await GetDocumentPath(new CancellationTokenSource());
    }

    private async Task<string> GetDocumentPath(CancellationTokenSource TokenSource)
    {
        var path = string.Empty;
        var LoopCheck = true;
        while (LoopCheck)
        {
            for (var i = 0; i < 100; i++)
            {
                if (MainActivity.fileUri != null || MainActivity.cancelled) { LoopCheck = false; TokenSource.Cancel(); }
                try { await Task.Delay(1000, TokenSource.Token); }
                catch (TaskCanceledException) { break; }
            }
        }
        if (!MainActivity.cancelled)
        {
            path = MainActivity.fileUri.OriginalString;
        }
        MainActivity.fileUri = null;
        MainActivity.cancelled = false;

        return path; 
    }
}

位于OnActivityResult ....

MainActivity.cs方法
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
     base.OnActivityResult(requestCode, resultCode, data);

     if (resultCode == Result.Ok && requestCode == DOCUMENT_CODE) { fileUri = new Uri(data.Data.Path); }
     else if(resultCode == Result.Canceled) { cancelled = true; }
}

我得到的一些路径......

data.DataString = content://com.android.externalstorage.documents/document/primary%3ADownload%2FSampleResume%20(1).docx

data.Data.SchemeSpecificPart = //com.android.externalstorage.documents/document/primary:Download/SampleResume (1).docx

data.Data.Path = /document/primary:Download/SampleResume (1).docx

然后还有一些编码选项。但是当我尝试使用...

读取它时,所有这些都会给我一个错误
File.ReadAllBytes(selectedDocumentPath); 

编辑:我已将fileUri = new Uri(data.Data.Path);替换为

var uri = DocumentsContract.BuildDocumentUri(data.Data.Authority,
                DocumentsContract.GetDocumentId(data.Data));                
fileUri = new Uri(uri.Path); 

但仍然提出与/document/primary:Download/SampleResume (1).docx

相同类型的路径

1 个答案:

答案 0 :(得分:1)

我在以下位置找到了解决方案:https://codereview.stackexchange.com/questions/182082/reading-file-from-flash-with-xamarin-forms-on-android-using-actionopendocument-i

基本上是从master(如果您以这种方式接收文件),或者是任何类型为Intent.Data的{​​{1}},例如: content:// com。 android.providers.downloads.documents / document / 13

您应该执行以下操作来读取文件:

Android.Net.Uri