Uros
Uros

Reputation: 2150

How to get permission to execute su command from Xamarin Android

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.

Upvotes: 1

Views: 802

Answers (1)

Elvis Xia - MSFT
Elvis Xia - MSFT

Reputation: 10841

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.

Upvotes: 1

Related Questions