hi everybody,
I’m trying to write a file to the Application.dataPath and to the sdcard but both of it doesn’t work.
I’ve been searching for quite a while now, but it seems that this topic is not really well covered yet.
To do this I slightly modified this script from the Scripting Reference:
// Saves screenshot as PNG file.
import System.IO;
// Take a shot immediately
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "write")) {
print ("You clicked the button!");
UploadPNG ();
}
}
function UploadPNG () {
// We should only read the screen bufferafter rendering is complete
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
tex.Apply ();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy (tex);
// For testing purposes, also write to a file in the project folder
//File.WriteAllBytes(Application.dataPath + "/SavedScreen.png", bytes);
File.WriteAllBytes("/sdcard/testfolder/testWrite.png", bytes);
}
When I press tho button nothing happens and in the LogCat I get the following messages:
for saving to sdcard:
-for saving to Application.dataPath:
My scripting skills are very basic so maybe I do something completely wrong, but I need to figure this out.
Please help me!
Thanks a lot,
Application.dataPath should never be used for write access. The reason for this is simple: Application.dataPath points to the actual .apk (which essentially is a .zip file). (I would actually argue that Application.dataPath should not be used for write access on any platform, for various reasons.)
Write access to sd-card is possible just as you described, but as you see you get an UnauthorizedAccessException. The reason for this is that sd-card access is controlled by the manifest permissions. The default manifest created by Unity does not include the android.permission.WRITE_EXTERNAL_STORAGE which enables sd-card access. If you override the manifest you can add this permission yourself.
With Unity 3.2 we added a check-box to control this more easily.
Thank you very much for your answer. I tried to change the manifest and was able to save the .png to the SD card. This was my preferred method anyway. I’m looking forward to work with 3.2. Seems to bring a lot of improvements!
It’s recommended to use Application.persistentDataPath on all platforms.
Using .dataPath on Windows will output files next to the binaries which is usually not what you want; instead you want it tied to the current user.
In Unity 3.3, on iPhone .persistentDataPath and concat’ing “Documents” to .dataPath should result in the same thing, thus making the string juggling unnecessary.
Ultimately Application.persistentDataPath should do the best thing on all platforms, which removes the need of having #ifdefs in the code.
I doubt that all developers want their screenshots showing up in the DCIM/Camera folder
If you want them to show up there, just move the file after Application.CaptureScreenshot is done.
Hi I have been following this post and have already done all the necessary steps to get my image into the DCIM/Camera folder but when i go to my Gallery and have a look at the pictures in there my downloaded image is not there however if i use the ES Explorer and navigate to DCIM/Camera the image is there. Where do i need to place the image so that users without ES Explorer can view the image directly from the gallery.
Have you figured this out by chance? Or can anyone answer this? I’m having the same issue. I want to be able to save an image file, not directly into the gallery folder, but a folder that is at least easy for the user to find on Android. I can save the file fine, but it’s only visible via ES Explorer and not the standard file explorer.
EDIT: Actually, a couple things going on here. ES Explorer is being deceiving. What’s happening is that the file is actually being stored under “Device Storage”, not the SDCARD. ES Explorer is showing the folder/file from the device storage as a whole, but it makes it look like it’s on the SDCARD because ES Explorer indicates /sdcard/ as it’s browsing directory. So. That said, the folder/file shows fine with file explorer, but still working on getting it to be created on the SDCARD, not the device storage. I’ve already added the WRITE_EXTERNAL_STORAGE, but still a no-go. I’ll update this post if I find the final answer.