[CB-3569] Allow FileTransfer.upload to reference android_assets

This commit is contained in:
Ian Clelland 2013-06-03 15:03:47 -04:00
parent c509c8e7e5
commit c28a313374
2 changed files with 18 additions and 17 deletions

View File

@ -94,12 +94,18 @@ public class FileHelper {
if (uriString.startsWith("content:")) { if (uriString.startsWith("content:")) {
Uri uri = Uri.parse(uriString); Uri uri = Uri.parse(uriString);
return cordova.getActivity().getContentResolver().openInputStream(uri); 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://")) { } 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 { } else {
return null; return null;
} }

View File

@ -855,20 +855,15 @@ public class FileTransfer extends CordovaPlugin {
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
private InputStream getPathFromUri(String path) throws FileNotFoundException { private InputStream getPathFromUri(String path) throws FileNotFoundException {
if (path.startsWith("content:")) { try {
Uri uri = Uri.parse(path); InputStream stream = FileHelper.getInputStreamFromUriString(path, cordova);
return cordova.getActivity().getContentResolver().openInputStream(uri); if (stream == null) {
} return new FileInputStream(path);
else if (path.startsWith("file://")) {
int question = path.indexOf("?");
if (question == -1) {
return new FileInputStream(path.substring(7));
} else { } else {
return new FileInputStream(path.substring(7, question)); return stream;
} }
} } catch (IOException e) {
else { throw new FileNotFoundException();
return new FileInputStream(path);
} }
} }