本文介绍了保存位图文件 - Xamarin,MonoDroid的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个位图图像保存到我的手机里面的一个目录(库)。该应用程序正在开发中Xamarin所以code是C#。

我似乎无法弄清楚如何创建一个目录,并保存位图。有什么建议?

 公共无效createBitmap(查看视图){view.DrawingCacheEnabled = TRUE; view.BuildDrawingCache(真);位图m_Bitmap = view.GetDrawingCache(真);    字符串storagePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    java.io.File的storageDirectory =新的java.io.File(storagePath);
    //storageDirectory.mkdirs();    //保存位图
    // MemoryStream的流=新的MemoryStream();
    //m_Bitmap.Com$p$pss(Bitmap.Com pressFormat.Png,100,流);
    //stream.Close();    尝试{        串文件路径= storageDirectory.ToString()+APPNAME.png;
        FOS的FileOutputStream =新的FileOutputStream(文件路径);
        BOS的BufferedOutputStream =新的BufferedOutputStream(FOS);
        m_Bitmap.Com preSS(Bitmap.Com pressFormat.Png,100,BOS);
        bos.Flush();
        bos.Close();
    }赶上(Java.IO.FileNotFoundException E){
        的System.Console.WriteLine(FILENOTFOUND);
    }赶上(java.io.IOException异常五){
        的System.Console.WriteLine(IOException异常);
    }


解决方案

这这里是一个超薄办法导出位图 PNG -file使用SD卡仅 C#的东西:

 无效ExportBitmapAsPNG(位图位图)
{
    VAR sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    变种文件路径= System.IO.Path.Combine(sdCardPathtest.png);
    VAR流=新的FileStream(文件路径,FileMode.Create);
    bitmap.Com preSS(Bitmap.Com pressFormat.Png,100,流);
    stream.Close();
}

I am trying to save a bitmap image to a directory (gallery) inside my phone. The app is being developed in Xamarin so the code is C#.

I cant seem to figure out how to make a directory, and save a Bitmap. Any suggestions?

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");
    }
解决方案

This here is a slim way to export a Bitmap as PNG-file to the sd-card using only C# stuff:

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();
}

这篇关于保存位图文件 - Xamarin,MonoDroid的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:35