将位图保存到文件 - Xamarin、Monodroid [英] Saving Bitmap to File - Xamarin, Monodroid
本文介绍了将位图保存到文件 - Xamarin、Monodroid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将位图图像保存到手机内的目录(图库)中.该应用程序是在 Xamarin 中开发的,因此代码是 C#.
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");
}
推荐答案
这是一种将 Bitmap
导出为 PNG
的slim 方式- 仅使用 C#
内容将文件复制到 sd 卡:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文