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

标签 c# android image xamarin

我正在使用适用于 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/

相关文章:

c# - 将 List<t> 转换或转换为 EntityCollection<T>

java - Android:在不耗尽内存的情况下将流转换为字符串

javascript - 我应该/如何使用 .slice() 在按下按钮时交换图像 scr? (用于图库中的下一个/上一个按钮)

android - 将特定文件复制到 Android 中的模拟器

html - 是否可以以正确的比例渲染两种尺寸的图像

python - 在 opencv python (cv2) 中调整透明图像的大小

c# - C++ 回调函数中的内存泄漏

c# - 插入到 Windows Azure 移动服务时出现 500 内部服务器错误?

c# - LINQ - 获取 2 个相反条件之间的最小计数

android - 如何查看特定播放列表的 youtube 列表?