Saving Bitmap to File - Xamarin, Monodroid

前端 未结 2 1631
渐次进展
渐次进展 2020-12-17 23:31

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 t

相关标签:
2条回答
  • Change:

    String filePath = storageDirectory.ToString() + "APPNAME.png";
    

    To:

    String filePath = Path.Combine(storageDirectory.ToString(), "APPNAME.png");
    

    Your original code appends the file name to the last folder in the path name without adding a path separator. For example, path of \data\data\sdcard01 would create a file path of \data\data\sdcard01APPNAME.png. Using Path.Combine() ensures that a path separator is used when appending directories.

    0 讨论(0)
  • 2020-12-18 00:11

    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();
    }
    
    0 讨论(0)
提交回复
热议问题