gpt4 book ai didi

xamarin - 使用 Xamarin 读取 android 文件

转载 作者:行者123 更新时间:2023-12-04 17:58:12 24 4
gpt4 key购买 nike

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

最佳答案

我在以下位置找到了解决方案: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.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com