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

Copying large files from StreamingAssets to Android persistent data

Discussion in 'Scripting' started by mediumTaj, Jul 2, 2015.

  1. mediumTaj

    mediumTaj

    Joined:
    Feb 20, 2015
    Posts:
    28
    Hello,
    I'm trying to copy files over from my StreamingAssets directory to Android persistent data via www. Small files transfer fine but when I switch to a large file I get the following error:

    You are trying to load data from a www stream which had the following error when downloading
    jar:file:///data/app/[.......].m4v aborted

    A small .mov file copies and plays perfectly. A large m4v file errors out. It appears to have started trying to copy the file over but when I check on the device, the video file is 0k.

    The code I'm using to copy over is as follows

    Code (CSharp):
    1. String fromPath = "jar:file://" + Application.dataPath + "!/assets/";
    2.         String toPath = Application.persistentDataPath +"/";
    3.  
    4.        
    5.         String[] filesNamesToCopy = new string[] {filePath};
    6.         foreach (String fileName in filesNamesToCopy)
    7.         {
    8.             if (!System.IO.File.Exists (toPath + fileName)) {
    9.                 Debug.Log("copying from "+ fromPath + fileName +" to "+ toPath);
    10.                 WWW www1 = new WWW( fromPath + fileName);
    11.                 yield return www1;
    12.                 Debug.Log("yield done");
    13.                 File.WriteAllBytes(toPath + fileName, www1.bytes);
    14.                 Debug.Log("file copy done");
    15.                 www1.Dispose ();
    16.                 www1 = null;
    17.             } else {
    18.                 Debug.Log("file exists! " + toPath + fileName);
    19.             }
    20.         }
    Any help would be greatly appreciated!!

    Thanks!
     
  2. JonWei

    JonWei

    Joined:
    Jan 26, 2015
    Posts:
    2
    Did you solve your problem?
     
  3. jxxxxst

    jxxxxst

    Joined:
    Nov 3, 2012
    Posts:
    50
    Bumping this, could you solve the problem? Having the same problem.
     
  4. mediumTaj

    mediumTaj

    Joined:
    Feb 20, 2015
    Posts:
    28
    My issue ended up being the file was too large to copy over to persistent data. Logcat showed an error that android could not allocate memory. I switched to a smaller video and it worked fine.
     
  5. shadows_s

    shadows_s

    Joined:
    Oct 7, 2013
    Posts:
    4
    Try to copy file per blocks:
    Code (csharp):
    1.  
    2. Streamstream = newMemoryStream(movie.bytes);
    3.  
    4. using(FileStreamoutput = newFileStream( videoPath, FileMode.Create, FileAccess.Write ))
    5. {
    6. byte[] buffer = newbyte[ 32 * 1024 ]; //a32KB-sizedbufferisthemostefficient
    7. intbytesRead;
    8.  
    9. while( (bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 ) {
    10. output.Write( buffer, 0, bytesRead );
    11.  }
    12. output.Flush();
    13.  }
    14.  
     
    jxxxxst likes this.