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");
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…