Google map screenshot always black screen

abhishekgoutamabhishekgoutam USMember ✭✭
edited March 4 in Xamarin.Forms

I want to take google map screenshot in my xamarin PCL Project. The map is displaying perfectly. But when i try to take a screenshot it is displaying a black screen for the map part. But extra part of the screen is capture perfectly. When i try to take screenshot from another app, i can take perfectly. I am using the below code.

My interface

public interface IScreenshotService { Task<byte[]> Capture(); }
`
public class ScreenshotService : IScreenshotService
{
public static Activity Activity { get; set; }

public async System.Threading.Tasks.Task<byte[]> Capture()
{
    if (Activity == null)
    {
        throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
    }

    var view = Activity.Window.DecorView;
    view.DrawingCacheEnabled = true;

    Bitmap bitmap = view.GetDrawingCache(true);

    byte[] bitmapData;

    using (var stream = new MemoryStream())
    {
        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
        bitmapData = stream.ToArray();
    }

    return bitmapData;
}

}

`

Code to for calling function

byte[] screenshotData = await DependencyService.Get<IScreenshotService>().Capture();

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited March 4

    Android Documentation:

    public void setDrawingCacheEnabled (boolean enabled)

    This method was deprecated in API level 28.
    The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache layers are largely unnecessary and can easily result in a net loss in performance due to the cost of creating and updating the layer. In the rare cases where caching layers are useful, such as for alpha animations, setLayerType(int, Paint) handles this with hardware rendering. For software-rendered snapshots of a small part of the View hierarchy or individual Views it is recommended to create a Canvas from either a Bitmap or Picture and call draw(Canvas) on the View. However these software-rendered usages are discouraged and have compatibility issues with hardware-only rendering features such as Config.HARDWARE bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback reports or unit testing the PixelCopy API is recommended.

  • abhishekgoutamabhishekgoutam USMember ✭✭

    i have already included
    view.DrawingCacheEnabled = true;

    in my code. Please take a look.

Sign In or Register to comment.