Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with access/write files on Android

Discussion in 'Scripting' started by stefann, Jan 4, 2011.

  1. stefann

    stefann

    Joined:
    Jan 4, 2011
    Posts:
    17
    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:
    Code (csharp):
    1. // Saves screenshot as PNG file.
    2. import System.IO;
    3.  
    4. // Take a shot immediately
    5. function OnGUI () {
    6.     if (GUI.Button (Rect (10,10,150,100), "write")) {
    7.         print ("You clicked the button!");
    8.         UploadPNG ();
    9.     }
    10. }
    11.  
    12. function UploadPNG () {
    13.     // We should only read the screen bufferafter rendering is complete
    14.     yield WaitForEndOfFrame();
    15.    
    16.     // Create a texture the size of the screen, RGB24 format
    17.     var width = Screen.width;
    18.     var height = Screen.height;
    19.     var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
    20.     // Read screen contents into the texture
    21.     tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
    22.     tex.Apply ();
    23.  
    24.     // Encode texture into PNG
    25.     var bytes = tex.EncodeToPNG();
    26.     Destroy (tex);
    27.  
    28.     // For testing purposes, also write to a file in the project folder
    29.     //File.WriteAllBytes(Application.dataPath + "/SavedScreen.png", bytes);
    30.     File.WriteAllBytes("/sdcard/testfolder/testWrite.png", bytes);
    31.    
    32. }
    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,

    Stefan
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Did you try posting in the Android sector of the forum they may know over there.
     
  3. stefann

    stefann

    Joined:
    Jan 4, 2011
    Posts:
    17
    Not so far, thanks for the idea...
    I'll post it there.