From bb777c096c981b163ea70483531d800e414ddf0b Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 22 Sep 2011 04:46:43 +0800 Subject: [PATCH] Fix for Issue #172: Out of memory when uploading video using FileTransfer on Android --- framework/assets/js/filetransfer.js | 6 +++++- framework/src/com/phonegap/FileTransfer.java | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/framework/assets/js/filetransfer.js b/framework/assets/js/filetransfer.js index 3d12d1d8..5db046ae 100644 --- a/framework/assets/js/filetransfer.js +++ b/framework/assets/js/filetransfer.js @@ -53,10 +53,14 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro var fileName = null; var mimeType = null; var params = null; + var chunkedMode = true; if (options) { fileKey = options.fileKey; fileName = options.fileName; mimeType = options.mimeType; + if (options.chunkedMode) { + chunkedMode = options.chunkedMode; + } if (options.params) { params = options.params; } @@ -65,7 +69,7 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro } } - PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, debug]); + PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, debug, chunkedMode]); }; /** diff --git a/framework/src/com/phonegap/FileTransfer.java b/framework/src/com/phonegap/FileTransfer.java index 6a0059b6..25c2929d 100644 --- a/framework/src/com/phonegap/FileTransfer.java +++ b/framework/src/com/phonegap/FileTransfer.java @@ -81,9 +81,10 @@ public class FileTransfer extends Plugin { try { JSONObject params = args.optJSONObject(5); boolean trustEveryone = args.optBoolean(6); + boolean chunkedMode = args.getBoolean(7); if (action.equals("upload")) { - FileUploadResult r = upload(file, server, fileKey, fileName, mimeType, params, trustEveryone); + FileUploadResult r = upload(file, server, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode); Log.d(LOG_TAG, "****** About to return a result from upload"); return new PluginResult(PluginResult.Status.OK, r.toJSONObject()); } else { @@ -202,7 +203,7 @@ public class FileTransfer extends Plugin { * @return FileUploadResult containing result of upload request */ public FileUploadResult upload(String file, String server, final String fileKey, final String fileName, - final String mimeType, JSONObject params, boolean trustEveryone) throws IOException, SSLException { + final String mimeType, JSONObject params, boolean trustEveryone, boolean chunkedMode) throws IOException, SSLException { // Create return object FileUploadResult result = new FileUploadResult(); @@ -240,7 +241,7 @@ public class FileTransfer extends Plugin { conn = https; } } - // Return a standard HTTP conneciton + // Return a standard HTTP connection else { conn = (HttpURLConnection) url.openConnection(); } @@ -264,7 +265,12 @@ public class FileTransfer extends Plugin { if (cookie != null) { conn.setRequestProperty("Cookie", cookie); } - + + // Should set this up as an option + if (chunkedMode) { + conn.setChunkedStreamingMode(maxBufferSize); + } + dos = new DataOutputStream( conn.getOutputStream() ); // Send any extra parameters @@ -357,6 +363,9 @@ public class FileTransfer extends Plugin { Uri uri = Uri.parse(path); return ctx.getContentResolver().openInputStream(uri); } + else if (path.startsWith("file://")) { + return new FileInputStream(path.substring(6)); + } else { return new FileInputStream(path); }