Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question [SOLVED] UNITY_ANDROID Screenshot Error

Discussion in 'Scripting' started by RDS_exclusory, May 10, 2023.

  1. RDS_exclusory

    RDS_exclusory

    Joined:
    Aug 15, 2020
    Posts:
    6
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.             string filePath = Directory.GetCurrentDirectory() + "/Assets/Resources/Screenshots";
    3.         #elif UNITY_ANDROID
    4.             //string filePath = GetAndroidFriendlyFilesPath();
    5.             string filePath = Application.persistentDataPath;
    6.         #endif
    7.      
    8.         ScreenCapture.CaptureScreenshot(Path.Combine(filePath, "Level-Screenshot.png"));
    This is a part of my code and if I run it in UNITY_EDITOR (on pc in editor) everithing work perfect but in UNITY_ANDROID (on android phone in build version) does not take a screenshot...

    I even add this to AndroidManifest.xml
    Code (JavaScript):
    1. <manifest>
    2. ...
    3.  
    4.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    5.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    6. ...
    7. </manifest>
    and doesnt work... pls help..
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,160
    Check the
    adb logcat
    console output... it's probably throwing an exception you can reason about.

    ALSO: this image will be written nowhere that the operating system can see it, so I'm curious how you define "doesn't work." Did you check if the target file exists afterwards? I bet it does.

    If you need the pic to end up in your camera roll, hurry to Youtube because that's a LOT of little details to get correct.

    Otherwise, time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. RDS_exclusory

    RDS_exclusory

    Joined:
    Aug 15, 2020
    Posts:
    6
    I use script to see console when in build game on phone and i see console log...
    Code (CSharp):
    1. ScreenCapture.CaptureScreenshot(Path.Combine(filePath, "Level-Screenshot.png"));
    2.  
    3. Debug.Log("SCREENSHOT!!!!");
    and after that Log "SCREENSHOT!!!!" in console there is FileNotFoundException: Could not find file "data/data/com.RDS.EmojiSwitchPaid/files/Level-Screenshot.png"

    so that script/code is running but there is other problem...

    And I try it again in EDITOR it work perfect
     
    Last edited: May 10, 2023
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,160
    Well the obvious question is, "What happens when you just write a file to that location?"

    eg, just use System.IO.File.WriteAllBytes() or something and see if you can write a file.

    Already the file path looks sketchy because it is relative. According to the docs it is supposed to be absolute:

    https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

    A relative path would be completely dependent on you never changing the working directory.

    EDIT: just verified on my Android Moto G (2021) that I get this absolute path:

    screen.png

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. // @kurtdekker - drop this on a GameObject that has a Text Component on it
    7.  
    8. public class DisplayApplicationPersistentDataPath : MonoBehaviour
    9. {
    10.     void Start ()
    11.     {
    12.         var apdp = Application.persistentDataPath;
    13.  
    14.         string s = "persistentDataPath:\n'" + apdp + "'\n\n";
    15.  
    16.         var t = GetComponent<Text>();
    17.  
    18.         t.text = s;
    19.     }
    20. }
     
    Last edited: May 11, 2023
  5. RDS_exclusory

    RDS_exclusory

    Joined:
    Aug 15, 2020
    Posts:
    6
    When I just add
    Code (CSharp):
    1. byte[] bytes = Encoding.UTF8.GetBytes("This is a string in C Sharp");
    2.      
    3.         System.IO.File.WriteAllBytes(Application.persistentDataPath, bytes);
    in build android version it print error "UnauthorizedAccessException: Access to path 'storage/emulated/0/Android/data/com.RDS.EmojiSwitchPaid/files' is denied."
    How to correct this?

    and your script prints "persistentDataPath: 'storage/emulated/0/Android/data/com.RDS.EmojiSwitchPaid/files'"
     
    Last edited: May 12, 2023
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,160
    That's weird... look closely at my screenshot: my version of Unity returns a path that starts with a slash!!

    Perhaps try just adding a slash at the beginning??? This seems VERY mysterious as to why you are getting relative paths. I wonder if it is a new Unity bug.

    Perhaps try it in a different version of Unity? If it is a bug, then file a bug with the Help -> Report A Bug menu item
     
  7. RDS_exclusory

    RDS_exclusory

    Joined:
    Aug 15, 2020
    Posts:
    6
    Im sorry my path start with / so it was "/storage/emulated/0/Android/data/com.RDS.EmojiSwitchPaid/files"
     
    Last edited: May 12, 2023
  8. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,057
    that isn't weird with
    System.IO.File.WriteAllBytes(Application.persistentDataPath, bytes);

    You're trying to write to the location that is a directory. If you've had written
    $"{Application.persistentDataPath}/testfile.bytes"
    it should probably work.
    WriteAllBytes requires an absolute file path. You are using a directory path in this case.

    For the original problem, after reading https://docs.unity3d.com/ScriptReference/ScreenCapture.CaptureScreenshot.html
    So on Android you will only need
    "level_screenshot.png"
    .

    Also worth noting:
     
    Last edited: May 12, 2023
  9. RDS_exclusory

    RDS_exclusory

    Joined:
    Aug 15, 2020
    Posts:
    6
    Thanks, now it work
     
    Last edited: May 13, 2023