locked
Take a screenshot RRS feed

  • Question

  • User99941 posted

    Hi, how can I take a screenshot in Xamarin Android ? Thanks !

    Friday, January 16, 2015 1:51 PM

All replies

  • User99941 posted

    I just find a way to achieve this, so here is my solution if someone need it. I wanted to make a screenshot in a Xamarin Forms project, and so, needed to make a custom service implementation both on Android and iOS.

    On iOS, no difficulties :

    UIScreen.MainScreen.Capture(); capture.SaveToPhotosAlbum((image, error) => { });

    On Android, here's how I achieve this :

    In the MainActivity, I override the OnCreateView(...) method to get the context :

    `public static Android.Content.Context Context { get; private set; }

    public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs) { MainActivityContext = context; return base.OnCreateView(parent, name, context, attrs); }`

    Then, I wrote a service implemenatation in which I take a screen capture, by using the static Context of the MainActivity, like this :

    `public class SnapshotService : ISnapshotService { public void Capture() { var screenshotPath = Android.OS.Environment.GetExternalStoragePublicDirectory("Pictures").AbsolutePath +
    Java.IO.File.Separator + "screenshot.png"; var rootView = ((Android.App.Activity)MainActivity.Context).Window.DecorView.RootView;

        using (var screenshot = Android.Graphics.Bitmap.CreateBitmap(
                rootView.Width, 
                rootView.Height, 
                Android.Graphics.Bitmap.Config.Argb8888))
        {
            var canvas = new Android.Graphics.Canvas(screenshot);
            rootView.Draw(canvas);
    
            using (var screenshotOutputStream = new System.IO.FileStream(
                        screenshotPath, 
                        System.IO.FileMode.Create))
            {
                screenshot.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 90, screenshotOutputStream);
                screenshotOutputStream.Flush();
                screenshotOutputStream.Close();
            }
        }
    }
    

    }`

    Hope it will help....

    Monday, January 19, 2015 10:44 AM
  • User68549 posted

    Thanks Rano It works for me

    Friday, May 8, 2015 4:00 PM
  • User174344 posted

    I've one question. ISnapShotService doesnt work here? Why?

    Friday, April 1, 2016 9:27 PM
  • User174344 posted

    @MilanGohil said: Thanks Rano It works for me

    I've one question. ISnapShotService doesnt work here? What should I do?

    Sunday, April 3, 2016 10:17 AM
  • User39110 posted

    @Rano
    Thanks Dear it saved me a lot of time, it works with android fine, I will see with iOS later

    Wednesday, June 29, 2016 11:01 AM
  • User256979 posted

    Hi Rano!

    It works but i need to send the context by param.

    thanks!

    Wednesday, September 14, 2016 11:27 AM
  • User64909 posted

    @Rano Thanks that was helpful.

    I am also looking for opening the image after saving it.

    In case of android it looks easy, for iOS :?

    Monday, September 26, 2016 1:28 AM
  • User306900 posted

    Could anyone please let me know how to email screen shot in IOS without saving image?

    Monday, March 13, 2017 10:38 AM
  • User272070 posted

    The only problem I have been getting is I have a VideoView which is playing video in it , but the screenshots have black screen in it.

    Tuesday, March 14, 2017 11:49 AM
  • User338446 posted

    Thanks It works !, but it does not capture the image I have on the surface view

    Tuesday, August 1, 2017 4:17 PM