- Home /
Android Native Screenshot function creats black Image
Hello Guys,
as written in the header I'm facing this weird problem that the Project is producing a black Screenshot when the native Android button combination is used (soundwhip + homebutton). Does anybody know why this happens? Does anybody know how I can enable the native screenshot function?
I added a script to make screenshots possible thru an UI Button on the GUI of the game but somehow the image is always kept in the Application.persistancepath even when the script tries to change its location.
public void CaptureScreenshot()
{
StartCoroutine(TakeScreenshot());
}
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
//INITIAL SETUP
screenShotCount++;
string myFilename ="screenshot" + screenShotCount + ".png";
string myDefaultLocation = Application.persistentDataPath + "/" + myFilename;
//EXAMPLE OF DIRECTLY ACCESSING THE Camera FOLDER OF THE GALLERY
//string myFolderLocation = "/storage/emulated/0/DCIM/Camera/";
//EXAMPLE OF BACKING INTO THE Camera FOLDER OF THE GALLERY
//string myFolderLocation = Application.persistentDataPath + "/../../../../DCIM/Camera/";
//EXAMPLE OF DIRECTLY ACCESSING A CUSTOM FOLDER OF THE GALLERY
string myFolderLocation = "/sdcard/DCIM/screenshotUnity/";
string myScreenshotLocation = myFolderLocation + myFilename;
//ENSURE THAT FOLDER LOCATION EXISTS
if (!System.IO.Directory.Exists(myFolderLocation))
{
System.IO.Directory.CreateDirectory(myFolderLocation);
}
//TAKE THE SCREENSHOT AND AUTOMATICALLY SAVE IT TO THE DEFAULT LOCATION.
Application.CaptureScreenshot(myFilename);
//MOVE THE SCREENSHOT WHERE WE WANT IT TO BE STORED
System.IO.File.Move(myDefaultLocation, myScreenshotLocation);
//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS BEGUN
AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2] { "android.intent.action.MEDIA_MOUNTED", classUri.CallStatic<AndroidJavaObject>("parse", "file://" + myScreenshotLocation) });
objActivity.Call("sendBroadcast", objIntent);
//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS COMPLETE
//AUTO LAUNCH/VIEW THE SCREENSHOT IN THE PHOTO GALLERY
//Application.OpenURL(myScreenshotLocation);
//AFTERWARDS IF YOU MANUALLY GO TO YOUR PHOTO GALLERY,
//YOU WILL SEE THE FOLDER WE CREATED CALLED "myFolder"
}
thank you
I still confronted with this Problem, would be nice if anyone had an answer....
Answer by MEDIALLIANCE_2 · Mar 06, 2017 at 03:22 PM
Hey, I think everyone struggles with screencapture on mobile devices... It's also melting my brain...
Anyway, here, you do'nt use Android Native function, you use Application.CaptureScreenshot(myFilename); and then you move the file to your desired location. The Android part is for updating the Photo Gallery (which doesn't work for me BTW).
Black screenshots seems to appear because of antialiasing. Try to remove it first to check if the issue is still here. I managed to find a workaround by checking the file size when created (black pictures are low sized), delete it and try again until completion. I had a limit where I removed antialiasing. File Size-Checking:
FileInfo fileInfo = new System.IO.FileInfo (sourcePath);
if (fileInfo.Length < 150000) { File.Delete (sourcePath); }
Hope this helps.
Thank you for your answer, I somehow managed to get the Screen capture going, the biggest Problem since today is that the Script won't move the Image into the Gallery folder /DCI$$anonymous$$, and it also won't refresh the Gallery so the Images can be shown.
This made me so frustrated that I just cancelled the Screen capture Functionality out of my Project, and after Updating to API Level 14 the Native Screen capture of Android started working properly. I just think I had something wrong in the Project Settings. I uploaded my Game to the Google PlayStore 2 weeks ago.
I'm facing the exact same problem with Gallery update. Not a single solution on the Internet works completely. Some assetstore packages/plugins seems to manage to do it. But I'm trying not to use plugin, since I'm facing problem when I got two running at the same time... I'm actually trying to use the Android Native takeScreenshot function. http://stackoverflow.com/questions/36385180/xamarin-android-take-screenshot
I finally bought this assets from the assetstore. It works fine on Android from screencapture and gallery update. You can also change the folder where the captures are taken (else it's the persistentDataPath)
thank you very much, that seems to be a good asset for the screencapture function