c# - 将位图保存到文件 - Xamarin、Monodroid

标签 c# android bitmap xamarin xamarin.android

我正在尝试将位图图像保存到手机中的目录(图库)中。该应用程序是在 Xamarin 中开发的,因此代码是 C#。

我似乎无法弄清楚如何制作目录并保存位图。有什么建议吗?

public void createBitmap(View view){ 
    view.DrawingCacheEnabled = true; 
    view.BuildDrawingCache (true); 
    Bitmap m_Bitmap = view.GetDrawingCache(true);

    String storagePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    Java.IO.File storageDirectory = new Java.IO.File(storagePath);
    //storageDirectory.mkdirs ();


    //save the bitmap
    //MemoryStream stream = new MemoryStream ();
    //m_Bitmap.Compress (Bitmap.CompressFormat.Png, 100, stream);
    //stream.Close();


    try{

        String filePath = storageDirectory.ToString() + "APPNAME.png";
        FileOutputStream fos = new FileOutputStream (filePath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        m_Bitmap.Compress (Bitmap.CompressFormat.Png, 100, bos);
        bos.Flush();
        bos.Close();
    } catch (Java.IO.FileNotFoundException e) {
        System.Console.WriteLine ("FILENOTFOUND");
    } catch (Java.IO.IOException e) {
        System.Console.WriteLine ("IOEXCEPTION");
    }

最佳答案

这是一个slim方法,仅使用C#<将位图导出为PNG文件到sd卡中 内容:

void ExportBitmapAsPNG(Bitmap bitmap)
{
    var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
    var stream = new FileStream(filePath, FileMode.Create);
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
    stream.Close();
}

关于c# - 将位图保存到文件 - Xamarin、Monodroid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976002/

相关文章:

C# 等效于 PHP 的原始输出 MD5?

c# - 检查数据库中的重复数据 (C#)

android - SQLite - 是否可以通过插入语句插入 BLOB?

android - 寻找 post/pre/set Translate 的解释(在 Matrix 对象中)以及如何使用它们

java - 不使用位图在游戏中设置背景

c# - 使用 HTTP 触发器读取和写入 Blob 的 Azure 函数

java - setSupportProgressBarInminatedVisibility 抛出 NPE(支持 V7 修订版 21.0.1)

android - ElementList SimpleXML 中的空条目

java - 创建位图并将其设置为操作栏图标。可能的?

c# - NUnit 测试中的 Lambda 和委托(delegate)类型 : compiler errors