c# - 在 Android 中将位图保存到图库
问题描述
我认为这应该很简单,但我无法弄清楚。我不断收到错误:System.IO.DirectoryNotFoundException: Could not find a part of the path "/storage/emulated/0/Pictures/Screenshots/name.jpg"
.
编码:
string root = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).Path;
File myDir = new File(root + "/Screenshots");
myDir.Mkdirs();
string timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").Format(new Date());
string fname = CommunicationHandler.GetNickname() + "|" + timeStamp + ".jpg";
File file = new File(myDir, fname);
if (file.Exists())
file.Delete();
try
{
using (System.IO.Stream outStream = System.IO.File.Create(file.Path))
{
finalBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream);
outStream.Flush();
outStream.Close();
}
}
catch (Exception e)
{
Toast.MakeText(Activity, e.ToString(), ToastLength.Long).Show();
}
另外,我无法手动访问 /storage/emulated/0..
为什么我无法将位图保存到我的手机图库中?上面的代码有什么问题?
解决方案
如果你想创建一个新目录,你可以使用 System.IO.Directory.CreateDirectory(root);
它来创建它。
//create a directory called MyCamera
string root = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim).ToString() + "/MyCamera/";
//create the Directory
System.IO.Directory.CreateDirectory(root);
推荐阅读
- python - 如何在熊猫中删除具有不同列名的重复数据?
- python - Python TCP套接字不会连接
- javascript - 来自客户端的快速呼叫服务器事件
- python - 列出每位获得奥斯卡奖的导演的获奖名单
- python - 只能运行一个 XML 文件,但不能加载所有文件
- javascript - 是否可以有条件地在 pdfMake 库中显示一些文本?
- android-studio-arctic-fox - Android Studio 北极狐 | 2020.3.1 补丁 2 更新
- c++ - 为什么 C++ 数组在第 4 个元素之后不起作用?
- python - 制作cnn+lstm模型时layer time_distributed的error input 0 is in compatible with the layer
- javascript - 如何在 ng-select 中初始设置一个值?