title: Save images on mobile from online source unity
body: <p>Main Goal : Load images from an online url then save the image (whatever type) locally in a /Repo/{VenueName} dir on a mobile.  This way it will hopefully save on future mobile data, when scene loads checks for local image first then calls a www request if it doesn't exist on the mobile already.</p>
  
  <p>I've got images online, I've pulled the url from a json file and now I want to store them locally on mobile devices to save data transfer for the end user.</p>
  
  <p>I've gone round in circles with persistent data paths and IO.directories and keep hitting problems.</p>
  
  <p>At the moment I've got a function that saves text from online and successfully stores it on a device but if I use it for images it won't work due to the string argument shown below, I tried to convert it to bytes editing the function too rather than passing it www.text and got an image corrupt error.</p>
  
  <p>Here's the old function I use for text saving files.</p>
  
  <pre><code>public void writeStringToFile( string str, string filename ){
          #if !WEB_BUILD
              string path = pathForDocumentsFile( filename );
              FileStream file = new FileStream (path, FileMode.Create, FileAccess.Write);
  
              StreamWriter sw = new StreamWriter( file );
              sw.WriteLine( str );
  
              sw.Close();
              file.Close();
          #endif  
      }
  
  
  public string pathForDocumentsFile( string filename ){ 
          if (Application.platform == RuntimePlatform.IPhonePlayer)
          {
              string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
              path = path.Substring( 0, path.LastIndexOf( '/' ) );
              return Path.Combine( Path.Combine( path, "Documents" ), filename );
          }else if(Application.platform == RuntimePlatform.Android){
          string path = Application.persistentDataPath;   
              path = path.Substring(0, path.LastIndexOf( '/' ) ); 
              return Path.Combine (path, filename);
          }else {
              string path = Application.dataPath; 
              path = path.Substring(0, path.LastIndexOf( '/' ) );
              return Path.Combine (path, filename);
          }
      }
  </code></pre>
  
  <p>This works well for text as it expect a string but I can't get it working on images no matter how much I edit it.</p>
  
  <p>I ended up going down a different route but have unauthorised access issues with the following code and don't think it will work on mobiles but..</p>
  
  <pre><code>IEnumerator loadPic(WWW www, string thefile, string folder)
   {
      yield return www;
       string venue = Application.persistentDataPath + folder;
       string path = Application.persistentDataPath + folder + "/" + thefile;
  
      if (!System.IO.Directory.Exists(venue))
       {
          System.IO.Directory.CreateDirectory(venue);
       }
  
      if (!System.IO.Directory.Exists(path))
       {
          System.IO.Directory.CreateDirectory(path);
       }
  
      System.IO.File.WriteAllBytes(path, www.bytes);
   }
  </code></pre>
  
  <p>Urgh, it's 3am here and I can't figure it out, can you wizards help me out?  Thanks in advance.</p>