- Home /
Black screenshot of the Unity app from Android
I made an android native plugin to take a screenshot. (Unity player 5.3 - Min version android API 19) I know it's possible to use Application.CaptureScreenshot or render a texture from Unity, but I really need to make my screenshot of my Unity app from android.
I created a java class with no heritage with the static method takeScreenshot :
View screenView = UnityPlayer.currentActivity.getWindow().getDecorView().getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
Date timeStamp = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", timeStamp);
String path = Environment.getExternalStorageDirectory().toString() + "/" + timeStamp + ".jpg";
File imageFile = new File(path);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
Then on Unity I have this onGUI :
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 150, 100), "screenShot")){
AndroidJavaObject jo = new AndroidJavaObject("MyScreenshotAndroidClass");
jo.CallStatic("takeScreenshot");
}
}
I have a picture saved on the device, with the good size, but always black. I don't know if it's a problem when I get the unity view, a problem when I create the bitmap or a problem when I write the file.
I tried to replace the getDrawingCache by using the Canvas :
Bitmap bitmap = Bitmap.createBitmap(screenView.getWidth(), screenView.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
screenView.draw(canvas);
But I had the same result.
Anyone have a clue to help me ?
Your answer
Follow this Question
Related Questions
Screenshot 0 Answers
Saving Screenshots to Android Gallery 8 Answers
Android obfuscator 1 Answer
YouTube Player Fragment in Plugin 1 Answer
Where can I find the resource definition for google play games android configuration? 1 Answer