【问题标题】:Reading an android file with Xamarin使用 Xamarin 读取 android 文件
【发布时间】:2016-07-25 19:02:03
【问题描述】:

这可能非常简单,但我无法从文件中读取字节。我让它在 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这样的相同类型的路径

【问题讨论】:

标签: xamarin xamarin.android


【解决方案1】:

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

基本上,来自Intent.Data(如果您以这种方式接收文件),或来自任何Android.Net.Uri 类型为content,例如:content://com.android.providers.downloads。文件/文件/13

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

using (var parcelFileDescriptor = ContentResolver.OpenFileDescriptor(Intent.Data, "r"))
using (Java.IO.FileDescriptor fileDescriptor = parcelFileDescriptor.FileDescriptor)
using (var reader = new Java.IO.FileReader(fileDescriptor))
using (var bufferedReader = new Java.IO.BufferedReader(reader))
{
    string line;
    while ((line = bufferedReader.ReadLine()) != null)
    {
        if (line != null)
        {

        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 2016-10-19
    • 2020-03-08
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多