bealtis
bealtis

Reputation: 25

Picture appears only after restart the device

I have an Android-App that takes pictures and saves them in an external storage ("DCIM/Cameras"). But the pictures appear only after restarting my handy. Is there some kind of Update or a way around this?

My source-code from saving my image:

var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
var pictures = dir.AbsolutePath + "/Camera";
string name = System.DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + ".jpg";
string filePath = System.IO.Path.Combine(pictures, name);
FileStream output;
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
try
{
    output = new FileStream(filePath, FileMode.Create);
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, output);
    output.Close();
    //Static Class that contains an methode for MediaScannerConnection.ScanFile 
    MediaGalleryHelper.AddFileToGallery(name); 
}
catch (System.Exception ex)
{
    System.Console.WriteLine(ex.ToString());
}

Upvotes: 1

Views: 68

Answers (1)

Inneke De Clippel
Inneke De Clippel

Reputation: 1026

You have to let the photo gallery know that a new photo has been added. You can use below code for that:

this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + filePath)));

Where 'this' is an activity.

Upvotes: 1

Related Questions