1

I have Xamarin PCL and in Android project I would like to take screen shot with code similar to this:

Java.Lang.Process sh = Java.Lang.Runtime.GetRuntime().Exec("su", null, null);
System.IO.Stream os = sh.OutputStream;
os.Write(Encoding.Unicode.GetBytes("/system/bin/screencap -p /sdcard/img.png"), 0, 0);
os.Flush();
os.Close();
sh.WaitFor();

but first line gives me exception: Java.IO.IOException: Permission denied

What can I do to get this permission?

I don't want to get screenshot with code like this:

var view = ((Android.App.Activity)MainActivity.Context).Window.DecorView.RootView;
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();
}

File.WriteAllBytes(screenshotPath, bitmapData);

because it doesn't give me time and other elements visible to user.

Uros
  • 2,099
  • 7
  • 23
  • 50

1 Answers1

1

What can I do to get this permission?

You will have to get your phone/emulator root permission. Without Root, su command is not available for your app.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24