Я пытаюсь сохранить на SD-карту и, если она недоступна, сохранить ее во внутренней памяти в Галерее, но всегда отображается в каталоге Galley. Но этот код не сохраняет его или как я могу сделать, чтобы показать его в каталоге галерей.
string nameFile = file + ".png";
//Check wether the sd card is available or not
string state = Android.OS.Environment.ExternalStorageState;
bool isExternalStorageAvailable = false;
bool isExternalStorageWritable = false;
string path = null;
if(Android.OS.Environment.MediaMounted.Equals(state))
{
isExternalStorageAvailable = isExternalStorageWritable = true;
//var path = Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic);
path = Android.OS.Environment.ExternalStorageDirectory.ToString();
Toast.MakeText(this, "Saved in SDCard", ToastLength.Short).Show();
}
else if(Android.OS.Environment.MediaMountedReadOnly.Equals(state))
{
isExternalStorageAvailable = true;
isExternalStorageWritable = false;
}
else
{
//path = Android.OS.Environment.DataDirectory;
path = Android.OS.Environment.DirectoryPictures;
Toast.MakeText(this, "Saved in Internal Memory ", ToastLength.Short).Show();
}
//Create a Bitmap screen capture
Android.Graphics.Bitmap bitmap;
View view = v.RootView;
view.DrawingCacheEnabled = true;
bitmap = Android.Graphics.Bitmap.CreateBitmap(view.DrawingCache);
view.DrawingCacheEnabled = false;
System.IO.FileStream fs = null;
try
{
fs = new System.IO.FileStream(path + "/" + nameFile, System.IO.FileMode.OpenOrCreate);
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, fs);
if(isExternalStorageAvailable == true && isExternalStorageWritable == true)
{
//SendBroadcast(new Intent(Intent.ActionMediaMounted, Android.Net.Uri.Parse(path + "/" + nameFile)));
MyMediaConnectorClient client = new MyMediaConnectorClient(path + "/" + nameFile);
MediaScannerConnection scanner = new MediaScannerConnection(this, client);
client.SetScanner(scanner);
scanner.Connect();
}
else
{
Android.Provider.MediaStore.Images.Media.InsertImage(ContentResolver, bitmap, nameFile, v.Id.ToString());
}
fs.Flush();
}
catch(FileNotFoundException e)
{
Log.Error("File Not Found Exception", e.ToString());
}
catch(IOException e)
{
Log.Error("IOException", e.ToString());
}
finally
{
if (fs != null)
fs.Close();
dialog.Dismiss();
}