Xamarin Android:System.IO.DirectoryNotFoundException:找不到路径的一



我有一个Xamarin.Forms Android应用程序,其中有问题的页面如下所示:

<Grid>
<ScrollView>
<StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
<Image x:Name="CapturedImage" Source="{Binding Capture}" HeightRequest="100" WidthRequest="100"/>
<Button Margin="0,10,0,0" Text="Capture photo"
Command="{Binding OpenCameraCommand}"
BackgroundColor="{StaticResource Primary}"
TextColor="White" />
<Label Text="{Binding Description}" />
</StackLayout>
</ScrollView>
</Grid>

用这个按钮我打开了Android原生摄像头,它可以工作。在ViewModel中,会发生以下情况:

public class CaptureViewModel : BaseViewModel
{
private string _description;
public string Description
{
get => _description;
set
{
_description = value;
OnPropertyChanged();
} 
}
private ImageSource _capture;
public ImageSource Capture
{
get => _capture;
set
{
_capture = value;
Description = value.ToString();
OnPropertyChanged();
}
}
public ICommand OpenCameraCommand { get; }

public CaptureViewModel()
{
Title = "Capture";
OpenCameraCommand = new Command(OnExecute);
}
private async void OnExecute()
{
await TakePhotoAsync();
}
private async Task TakePhotoAsync()
{
var mpo = new MediaPickerOptions
{
Title = "test"
};
var photo = await MediaPicker.CapturePhotoAsync(mpo);
await LoadPhotoAsync(photo);
var image = await Image.FromFileAsync(Capture.ToString());
var client = await ImageAnnotatorClient.CreateAsync();
var textAnnotations = await client.DetectTextAsync(image);
foreach (var text in textAnnotations)
{
Description = text.Description;
}
}
private async Task LoadPhotoAsync(FileResult photo)
{
if (photo == null)
{
Capture = null;
return;
}
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var newFile = Path.Combine(path, photo.FileName);
using (var stream = await photo.OpenReadAsync())
{
var newStream = File.OpenWrite(newFile);
await stream.CopyToAsync(newStream);
newStream.Close();
newStream.Dispose();
}
Capture = newFile;
}
}

实际上,我想使用VM中的ImageAnnotator,但在尝试获取捕获的图像进行处理时,我总是会遇到错误。

找不到路径

System.IO.DirectoryNotFoundException:"找不到路径的一部分"/文件:/data/user/0/companyname.app2/files/df033720bb9646928a9626e25e116990.jpg">

我在AndroidManifest.xml中的权限看起来像:

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="App2.Android" android:theme="@style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

我不知道还有什么问题,但我尝试了其他一些方法,但都无济于事。可能是什么问题?

编辑:顺便说一句,我可以在视图中看到捕获,出于调试原因,标签也显示了一个路径(与图片中相同(。

因此,这里的解决方案是使用返回的FileResult中的FullPath属性来获得将其转换为流所需的正确路径。

错误:

var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var newFile = Path.Combine(path, photo.FileName);

右图:

var photo = await MediaPicker.CapturePhotoAsync(mpo);
var photoFullPath = photo.FullPath;
Capture = photoFullPath;
var image = await Image.FromFileAsync(photoFullPath);

最新更新