gpt4 book ai didi

C#如何将图像保存到SD卡?

转载 作者:行者123 更新时间:2023-11-29 20:53:02 25 4
gpt4 key购买 nike

我正在使用适用于 Android 的 Xamarin。我加载图像并将其放入 ImageView,然后编辑图像。接下来我想将该图像保存到 SD 卡中。

任何人都知道如何将图像保存到 SD 卡中,因为我只能在 Java Android 中找到它。我已经尝试将代码从 Java 转换为 C#,但仍然出现错误。

任何帮助,在此先感谢。


我在 InputStream iS = Resources.OpenRawResource(Resource.Drawable.Icon); 处收到错误,因为错误是“无法将类型‘System.IO.Stream’隐式转换为‘Java.IO .输入流'"

代码如下:

Java.IO.File path = Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DirectoryPictures);
Java.IO.File file = new Java.IO.File (path, "Icon.png");

try {
path.Mkdirs();
InputStream iS = Resources.OpenRawResource(Resource.Drawable.Icon);
OutputStream oS = new FileOutputStream(file);
byte[] data = new byte[iS.Available()];
iS.Read(data);
oS.Write(data);
iS.Close();
oS.Close();
} catch (Exception ex) {
// ...
}

最佳答案

我用它来将拍摄的照片保存到 sdcard:

public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
{
// Save the image JPEG data to the SD card
FileOutputStream outStream = null;
File dataDir = Android.OS.Environment.ExternalStorageDirectory;
if (data!=null)
{
try
{
outStream = new FileOutputStream(dataDir + "/" + PICTURE_FILENAME);
outStream.Write(data);
outStream.Close();
}
catch (FileNotFoundException e)
{
Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
}
catch (IOException e)
{
Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
}
File file = new File(dataDir + "/" + PICTURE_FILENAME);
try
{
ExifInterface exif = new ExifInterface(file.CanonicalPath);
// Read the camera model and location attributes
exif.GetAttribute(ExifInterface.TagModel);
float[] latLng = new float[2];
exif.GetLatLong(latLng);
// Set the camera make
exif.SetAttribute(ExifInterface.TagMake, “My Phone”);
exif.SetAttribute(ExifInterface.TagDatetime,
System.DateTime.Now.ToString());
}
catch (IOException e) {
Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
}
}
else
{
Toast.MakeText(this, "No Image Captured", ToastLength.Long);
}
}

关于C#如何将图像保存到SD卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com