locked
Unable to see automatically in the gallery a image when saving to camera roll RRS feed

  • Question

  • User120838 posted

    Hello, Im doing a "Save to camera roll" method, and this work, the problem is that i can not see the image saved immediately, just after 1 -2 hours

    This is my code: public void SaveToCameraRoll(string fileImage, string fileName) { var uri = global::Android.Net.Uri.Parse (fileImage); var mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile); mediaScanIntent.SetData (uri);

            Bitmap imageBitmap = null;
    
            using (var webClient = new WebClient())
            {
                var imageBytes = webClient.DownloadData(fileImage);
                if (imageBytes != null && imageBytes.Length > 0)
                {
                    imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                }
            }
    
            String storagePath = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            var dir = new Java.IO.File (global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryPictures), "MyImages");
            dir.Mkdirs ();
            try{
    
                String filePath = System.IO.Path.Combine(dir.ToString(), fileName);
                if(System.IO.File.Exists(filePath))
                {
                    Toast.MakeText (Xamarin.Forms.Forms.Context, "Image was already downloaded", ToastLength.Long).Show ();
                    return;
                }
                using (var os = new FileStream(filePath, FileMode.CreateNew))
                {
                    imageBitmap.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
                }
            } catch (Java.IO.FileNotFoundException e) {
                System.Console.WriteLine ("FILENOTFOUND");
            } catch (Java.IO.IOException e) {
                System.Console.WriteLine ("IOEXCEPTION");
            }
            //var dir2 = global::Android.OS.Environment.GetExternalStoragePublicDirectory;
            Forms.Context.SendBroadcast (new Intent(Intent.ActionMediaScannerScanFile, global::Android.Net.Uri.Parse("file://" + dir.ToString())));
            SingleMediaScanner.NotifyFile(Forms.Context, "file://" + dir.ToString ());
            Toast.MakeText (Xamarin.Forms.Forms.Context, "Image saved to gallery", ToastLength.Long).Show ();
        }
    

    I think i need a way to tell to the Android Gallery to refresh

    Friday, May 1, 2015 6:06 PM

Answers

All replies