这可能非常简单,但我无法从文件中读取字节。我让它在 iOS 上运行良好,但在 Android 上它总是因 DirectoryNotFoundException
而崩溃。所以我认为 uri 是错误的,但我不确定我哪里错了......
我的 View 有一个打开文件选择器的按钮...
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;
}
}
位于MainActivity.cs
中的OnActivityResult
方法....
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
这样的路径
最佳答案
基本上,来自 Intent.Data
(如果您以这种方式收到文件),或来自任何 Android.Net.Uri
那是类型 content
,例如:content://com.android.providers.downloads.documents/document/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)
{
}
}
}
关于xamarin - 使用 Xamarin 读取 android 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575577/