簡體   English   中英

Xamarin Android:System.IO.DirectoryNotFoundException:找不到路徑的一部分

[英]Xamarin Android: System.IO.DirectoryNotFoundException: Could not find a part of the path

我有一個 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/com.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" />

我不知道還有什么問題,但是我嘗試了其他一些方法,但沒有幫助。 可能是什么問題?

編輯:順便說一句,我可以在視圖中看到 Capture。 出於調試原因,Label 也顯示了路徑(與圖片中的相同)

執行行時,可能目錄不存在。

通過首先創建目錄來解決此問題:

using System.IO;

    var newFile = ...;
    Directory.CreateDirectory(Path.GetDirectoryName(newFile));

如果目錄已經存在,這將什么也不做。


或者在你的情況下,它看起來像path是目錄,所以很簡單:

    Directory.CreateDirectory(path);

所以這里的解決方案是使用返回的FileResult中的FullPath屬性來獲取將其轉換為 stream 所需的正確路徑。

錯誤的:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM