Phonegap Build: File download only working on some Android devices (Apple is fine...)

69 views
Skip to first unread message

Trev Burley

unread,
Sep 10, 2014, 5:09:09 PM9/10/14
to phon...@googlegroups.com
Hi All,

Got an issue with the file download part of my app, during start up it fetches all of the files it needs to display to the user (for the entire app). This happens once for the entire install (and it downloads additional files when required during subsequent app start ups).

This works fine on my iPad and Nexus 5, but not on my Samsung Galaxy Note 10.1. The tablet says it is downloading the files but the ones it creates are around 20 bytes in size (whereas the other devices download the full files). I can only put this down to the fact the Nexus 5 uses Chrome for it's webviews but the Note 10.1 uses the old webview. Can anyone point me in the right direction please? I'm using the org.apache.cordova.file plugin version 1.0.1 (highest available on Phonegap Build) and have run out of options...

My 'download file' class is as follows:
var df_filetransfer=
{
Init: function (p_init) 
{

df_filetransfer.remote_filename = encodeURI(p_init.filename);
df_filetransfer.chunk_size = p_init.chunk_size;
df_filetransfer.on_success = p_init.success;
df_filetransfer.on_progress = p_init.progress;
df_filetransfer.on_fail = p_init.fail;
df_filetransfer.php_url = p_init.php_url;

// Note: The file system has been prefixed as of Google Chrome 12:
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
if (typeof LocalFileSystem == 'undefined')
{
window.webkitStorageInfo.requestQuota(PERSISTENT, 100*1024*1024, function(grantedBytes) 
{
  window.requestFileSystem(PERSISTENT, grantedBytes
                   ,df_filetransfer.onRequestFileSystemSuccess
                   ,df_filetransfer.onFail
               );
  }
  ,df_filetransfer.onFail
  );
}
else
{
window.requestFileSystem(
                   LocalFileSystem.PERSISTENT
                   ,0
                   ,df_filetransfer.onRequestFileSystemSuccess
                   ,df_filetransfer.onFail
               );
}
}
,onRequestFileSystemSuccess: function(p_fileSystem) 
{
console.log('onRequestFileSystemSuccess()');

df_filetransfer.fs = p_fileSystem;
df_filetransfer.lfn = df_filetransfer.remote_filename.substr(1+df_filetransfer.remote_filename.lastIndexOf("/"));
df_filetransfer.local_filename = df_filetransfer.fs.root.toURL()+df_filetransfer.lfn;

   df_filetransfer.fs.root.getFile(
                   df_filetransfer.lfn
                   ,{create: true,exclusive:false}
                   ,df_filetransfer.onGetFile
                   ,df_filetransfer.onFail
);
}

,onGetFile: function(p_FileEntry)
{
df_filetransfer.file=p_FileEntry;
df_filetransfer.file.createWriter(function(fileWriter) 
{
df_filetransfer.fileWriter = fileWriter;
df_filetransfer.fileWriter.onwriteend = function(e) 
df_filetransfer.fileWriter.onwriteend = function(e) 
if (df_filetransfer.response.last==1)
{
    df_filetransfer.status="COMPLETE";
    if (df_filetransfer.on_success)
    df_filetransfer.on_success();
}
else
{
    df_filetransfer.status="PROGRESS";
    if (df_filetransfer.on_progress)
    df_filetransfer.on_progress();
df_filetransfer.chunk_index++;
df_filetransfer.onNextChunk();
}
};

df_filetransfer.chunk_index=0;
df_filetransfer.downloaded=0;
df_filetransfer.onNextChunk();
};
    df_filetransfer.fileWriter.onerror = function(e) 
    {
    df_filetransfer.onFail(
    {
    reason: "local filesystem write failed"
    ,error: e
    });
    };
df_filetransfer.fileWriter.truncate(0);
},  df_filetransfer.onFail);
}

,onNextChunk: function ()
{
$.ajax(
{
type: "POST"
,url: df_filetransfer.php_url
,dataType: "json"
,data: { filename: df_filetransfer.remote_filename
,chunk_size: df_filetransfer.chunk_size
,chunk_index: df_filetransfer.chunk_index
,cache_name: df_filetransfer.cache_name
}
,success: df_filetransfer.onNextChunkSuccess
,error: function (e)
{
    df_filetransfer.onFail(
    {
    reason: e.responseText.reason
    ,error: e
    });
}
}); //.ajax
}

,onNextChunkSuccess: function (p_response) 
{
if (!p_response)
{
df_filetransfer.onFail({reason:"null response"});
return;
}
else if (p_response.status=="BAD")
{
df_filetransfer.onFail(p_response);
return;
}
if (df_filetransfer.chunk_index==0)
{
df_filetransfer.file_size=p_response.file_size;
}

df_filetransfer.response=p_response;
df_filetransfer.cache_name=p_response.cache_name;

var h4=p_response.h4;
var b = new ArrayBuffer(Math.floor(h4.length/2)); // 1 bytes for each char
var bufView = new Uint8Array(b);
var j=0;
for (var i= 0; i<h4.length; i+=2) 
{
   bufView[j++] = parseInt (h4.substr (i, 2), 16);
}

try {
var blob = new Blob([b]);//, {type: 'image/jpeg'});
console.log("Trying to use blob");
} catch(e) {
console.log("blob didn't work...");
// TypeError old chrome and FF
   window.BlobBuilder = window.BlobBuilder || 
                        window.WebKitBlobBuilder || 
                        window.MozBlobBuilder || 
                        window.MSBlobBuilder;
   if (e.name == 'TypeError' && window.BlobBuilder) {
    console.log("trying to use BlobBuilder");
       var bb = new BlobBuilder();
       bb.append([b]);
       var blob = bb.getBlob();
   }
   else if (e.name == "InvalidStateError") {
       console.log("Use blob anyway with the InvalidStateError");
       var blob = new Blob( [b] );
   }
   else {
       // We're screwed, blob constructor unsupported entirely   
       console.error("Error, blob data not supported!");
   }
}

df_filetransfer.last_chunk_size = blob.size;
df_filetransfer.downloaded += blob.size;
df_filetransfer.fileWriter.write(blob);
}

,onFail: function (p_fail)
{
df_filetransfer.status="BAD";
df_filetransfer.reason=p_fail.reason;
df_filetransfer.error=p_fail;
    if (df_filetransfer.on_fail)
    df_filetransfer.on_fail();
}
};

and I am using it as follows:
df_filetransfer.Init(
        {
            filename:       file_url
            ,php_url:       domain_url+"api/data_provider/_next_chunk.php"
            ,chunk_size:    2048*1024
            ,success:       _success_fn
            ,progress:      _progress_fn
            ,fail:          _fail_fn
        });

Any help would be much appreciated!

Thanks - Trev

Trev Burley

unread,
Sep 10, 2014, 5:25:39 PM9/10/14
to phon...@googlegroups.com
These 20 byte files contain just 

{"type":"","size":46379}

Kind Regards - Trev

Trev Burley

unread,
Sep 10, 2014, 7:37:41 PM9/10/14
to phon...@googlegroups.com
As far as I can tell it's falling over at fileWriter.write(blob); - the blob data seems correct at this stage, onwriteend fires but the data from the blob just isn't getting into the file hence assuming the issue is at this line.

Trev

Trev Burley

unread,
Sep 11, 2014, 9:39:51 AM9/11/14
to phon...@googlegroups.com
Gave up in the end, stopped using Phonegap Build for Android and switched to "Crosswalk - Cordova" instead which changes the Webview to a Chromium based webview, seen a massive improvement in performance and everything seems to work fine even without the plugins...

Trev
Reply all
Reply to author
Forward
0 new messages