From c28a3133743567808d022b04ce028c30cca86b83 Mon Sep 17 00:00:00 2001 From: Ian Clelland Date: Mon, 3 Jun 2013 15:03:47 -0400 Subject: [PATCH] [CB-3569] Allow FileTransfer.upload to reference android_assets --- .../src/org/apache/cordova/FileHelper.java | 16 +++++++++++----- .../src/org/apache/cordova/FileTransfer.java | 19 +++++++------------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/framework/src/org/apache/cordova/FileHelper.java b/framework/src/org/apache/cordova/FileHelper.java index 8bc24aa1..0d13c127 100644 --- a/framework/src/org/apache/cordova/FileHelper.java +++ b/framework/src/org/apache/cordova/FileHelper.java @@ -94,12 +94,18 @@ public class FileHelper { if (uriString.startsWith("content:")) { Uri uri = Uri.parse(uriString); return cordova.getActivity().getContentResolver().openInputStream(uri); - } else if (uriString.startsWith("file:///android_asset/")) { - Uri uri = Uri.parse(uriString); - String relativePath = uri.getPath().substring(15); - return cordova.getActivity().getAssets().open(relativePath); } else if (uriString.startsWith("file://")) { - return new FileInputStream(getRealPath(uriString, cordova)); + int question = uriString.indexOf("?"); + if (question > -1) { + uriString = uriString.substring(0,question); + } + if (uriString.startsWith("file:///android_asset/")) { + Uri uri = Uri.parse(uriString); + String relativePath = uri.getPath().substring(15); + return cordova.getActivity().getAssets().open(relativePath); + } else { + return new FileInputStream(getRealPath(uriString, cordova)); + } } else { return null; } diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java index 784c34c5..1b45fef2 100644 --- a/framework/src/org/apache/cordova/FileTransfer.java +++ b/framework/src/org/apache/cordova/FileTransfer.java @@ -855,20 +855,15 @@ public class FileTransfer extends CordovaPlugin { * @throws FileNotFoundException */ private InputStream getPathFromUri(String path) throws FileNotFoundException { - if (path.startsWith("content:")) { - Uri uri = Uri.parse(path); - return cordova.getActivity().getContentResolver().openInputStream(uri); - } - else if (path.startsWith("file://")) { - int question = path.indexOf("?"); - if (question == -1) { - return new FileInputStream(path.substring(7)); + try { + InputStream stream = FileHelper.getInputStreamFromUriString(path, cordova); + if (stream == null) { + return new FileInputStream(path); } else { - return new FileInputStream(path.substring(7, question)); + return stream; } - } - else { - return new FileInputStream(path); + } catch (IOException e) { + throw new FileNotFoundException(); } }