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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

External SD Card Access

Discussion in 'Android' started by Magius96, Jul 23, 2015.

  1. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    Part of the requirements for my app is that it stores it's data in the external SD card, not in the phone's internal SD card. This is because we can control the size of the external SD card, where we have no control over the size of the internal SD card. If you haven't figured it out, our application will generate a lot of images and other data that will be stored on the external SD card and uploaded to our server when the device is returned to our office.

    I have set the write access option in the player settings to "External (SDCard)", thinking that would allow me to write to the "/storage/extSdCard" path. While I can validate that the path exists, I am still getting an access denied error when attempting to write to that path.

    I've been googling and binging all morning for this, and still have gotten no where with it. Can someone please shed some light on this ASAP? My deadline is fast approaching in just 3 weeks, and I need to get this requirement set in and working.
     
  2. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    Pls post your tested phones and your code related to the problem :)
     
  3. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    This determines what the external file path is, it works, and on my Galaxy Mega it is returning the first option (which i know is there)

    private static string GetExternalFilePath()
    {
    if (Directory.Exists("/storage/extSdCard")) return "/storage/extSdCard";
    if (Directory.Exists("/storage/sdcard1")) return "/storage/sdcard1";
    if (Directory.Exists("/storage/usbcard1")) return "storage/usbcard1";
    if (Directory.Exists("/storage/sdcard0")) return "/storage/sdcard0";
    return Application.persistentDataPath;
    }

    I then build the path for my applcation's data:

    private static string _externalFilePath = "";
    public static string SiteVueDirectory
    {
    get
    {
    if (string.IsNullOrEmpty(_externalFilePath))
    {
    _externalFilePath = GetExternalFilePath();
    }
    return string.Format("{0}/SiteVue", _externalFilePath);
    }
    }

    Finally I build the test file's full path:

    public static string SiteListFile { get { return string.Format("{0}/sites.dat", SiteVueDirectory); } }

    With all that done, i do the following:

    if (!Directory.Exists(Globals.SiteVueDirectory)) Directory.CreateDirectory(Globals.SiteVueDirectory);

    string testString = "This is a test string";
    var txtFile = File.AppendText(Globals.SiteListFile);
    txtFile.Write(testString);
    txtFile.Close();

    However, i'm getting access denied errors when attempting to create the directory. I have even tried just saving the text file into the path that is stored into _externalFilePath, same thing, access denied errors.
     
  4. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    I don't normally bump stuff, but I'm quickly running out of time on this project and any help is appreciated. This IS the only requirement of the app that I'm struggling with.
     
  5. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    I just tested in on an galaxy grand prime. Writing an simple txt file, hardcoded, so no other bugs are likely. It failed too, so im sorry. But works on other phones like lg g2/3. Couldnt find an workaround :/
     
  6. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    470
    Hello,
    did someone found a solution?
    I also can not write to sd-card nor read from sd-card...
    nor is Unity installing the App on SD-Card... all options are checkt...
    duhhh these Unity 5.x specials drive me nuts....
    With Unity 4 all is fine...
     
  7. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    No. The end result that I had to accept is that we can't really control where the app data gets saved.