- Home /
Other
String.Format won't work on Android.
Hi, I was just wondering what could be preventing this to work on Android Devices. It takes the screenshot on PC inside the editor, but not on mobile.
The commented out line works fine on android.
Also the folder is created in the mobile environment but the screenshots are not saved there.
Is it wrong syntax for mobile?
Thanks, Tim.
import System.IO;
import System.Collections;
var folder = "";
var frameRate = 0;
function Start () {
Time.captureFramerate = frameRate;
System.IO.Directory.CreateDirectory(folder);
TakeScreenShot ();
}
function Awake (){
folder = Application.persistentDataPath + "/../../../../DCIM/TestShots/";
}
function TakeScreenShot (){
var info3 = new DirectoryInfo(folder);
var fileInfo3 = info3.GetFiles();
var name = String.Format("{0}/{1:D04} shot.png", folder, Time.frameCount );
Application.CaptureScreenshot(name);
// Application.CaptureScreenshot("Shot"+info3.GetFiles().Length +".png");
}
EDIT: I have this now, it will take and save in the correct location but now it gives the error, so the above question doesn't really matter
"FormatException: The specified format 'D04' is invalid"
So I changed the D04 to 004 and it only takes one screenshot.
var name = String.Format("{0}/{1:D04} shot.png", folder, frameRate );
Application.CaptureScreenshot(name);
How could I change this {0}/{1:D04}
to incorporate this part? As this would solve my problems.
var info3 = new DirectoryInfo(folder);
var fileInfo3 = info3.GetFiles();
Application.CaptureScreenshot("Shot"+info3.GetFiles().Length +".png");
Answer by Landern · Jul 14, 2014 at 12:21 PM
wrong format specification? Should it be "D4", meaning four decimal places from the right side.
From [Standard Numeric Format String on MSDN][1]:
The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros. Also see: [Composite Formatting][2] [1]: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString [2]: http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx Edit: Hrm, may be another issue, ran it in VS2013 and mimic'd your code and it worked fine with leading zeros on the format specifier... :(The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.
I found that Time.frameCount will deter$$anonymous$$e the number after the 0'z.
so it's going 00Shot"Time.frameCount".png. (00Shot12.png, 00Shot53.png) etc.
Also on mobile if I take a few shots, they aren't taken, but the button that allows the user to take a screenshot disappears.
I will do a log cat I think. forgot about that heheh.
I'm thinking the syntax is correct but something else may be preventing it from working on mobiles. I'l have a play around.
Here is what the log states.
E/Unity (20544): Failed to open file at path: /storage/sdcard0/Android/data/com.indevelope.Tandem$$anonymous$$ingz/files/storage/sdcard0/Android/data/com.indevelope.Tandem$$anonymous$$ingz/files/0560 shot.png
E/Unity (20544):
E/Unity (20544): (Filename: Line: 489)
E/Unity (20544):
E/Unity (20544): Failed to store screen shot
E/Unity (20544):
E/Unity (20544): (Filename: Line: 238)
E/Unity (20544):
I will just use a different method and see how they work. Something like:
var name = folder + info3.GetFiles().Length + "Shot.png";