我正在尝试将位图图像保存到手机中的目录(图库)中。该应用程序是在 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/