【问题标题】:Xamarin Could not find a part of the pathXamarin 找不到路径的一部分
【发布时间】:2021-04-20 21:44:50
【问题描述】:

我正在尝试将位图保存到 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:找不到路径的一部分。

感谢任何帮助。 在此先感谢 Zoedingl。

【问题讨论】:

  • @SushiHangover 是的,确实如此。
  • filePath 的值是多少?
  • @SushiHangover filePath的值为Android/data/com.oedingsoftware.retrogamez/files/Pictures/profile_picture.png
  • 当我创建文件夹Picture(如果文件夹存在,忽略创建文件夹),你的代码没有错误。请检查屏幕截图。 imgur.com/3GAZDZH
  • @Wendy Zang 我现在更加困惑。我在代码的另一部分创建文件夹。所以也许我应该像你一样把它移到那个地方?

标签: c# android file xamarin io


【解决方案1】:

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

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

创建目录的代码

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

【讨论】:

  • 文件夹已经存在,谢谢。
  • @Zoedingl 那么问题是什么?如果文件夹已经存在,则不应出现“找不到路径”问题。你能告诉我是什么问题吗?这可能会帮助遇到此类问题的其他人。
  • 我得到了确切的错误代码,我知道为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多