首页 > 解决方案 > Xamarin 找不到路径的一部分

问题描述

我正在尝试将位图保存到 Android 存储。我的代码如下所示:

var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(folderPath, "Pictures/profile_picture.png");
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();

但是应用程序进入中断模式并显示:

System.IO.DirectoryNotFoundException:找不到路径的一部分。

任何帮助表示赞赏。在此先感谢,佐丁格尔。

标签: c#androidfilexamarinio

解决方案


“图片”目录未创建。因此,您收到“找不到路径的一部分”错误。如下更改代码,或者您可以为“图片”创建目录,然后使用相同的代码应该可以工作。

var filePath = System.IO.Path.Combine(folderPath, "profile_picture.png");

创建目录的代码

 Directory.CreateDirectory(System.IO.Path.Combine(folderPath, "Pictures"));

推荐阅读