Copying large files from StreamingAssets to Android persistent data

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

String fromPath = "jar:file://" + Application.dataPath + "!/assets/";
        String toPath = Application.persistentDataPath +"/";

       
        String[] filesNamesToCopy = new string[] {filePath};
        foreach (String fileName in filesNamesToCopy)
        {
            if (!System.IO.File.Exists (toPath + fileName)) {
                Debug.Log("copying from "+ fromPath + fileName +" to "+ toPath);
                WWW www1 = new WWW( fromPath + fileName);
                yield return www1;
                Debug.Log("yield done");
                File.WriteAllBytes(toPath + fileName, www1.bytes);
                Debug.Log("file copy done");
                www1.Dispose ();
                www1 = null;
            } else {
                Debug.Log("file exists! " + toPath + fileName);
            }
        }

Any help would be greatly appreciated!!

Thanks!

Did you solve your problem?

Bumping this, could you solve the problem? Having the same problem.

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.

Try to copy file per blocks:

Streamstream = newMemoryStream(movie.bytes);

using(FileStreamoutput = newFileStream( videoPath, FileMode.Create, FileAccess.Write )) 
{
byte[] buffer = newbyte[ 32 * 1024 ]; //a32KB-sizedbufferisthemostefficient
intbytesRead;

while( (bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 ) {
output.Write( buffer, 0, bytesRead );
 }
output.Flush();
 }
1 Like