Created
June 22, 2013 22:25
-
-
Save timanglade/5842855 to your computer and use it in GitHub Desktop.
Camera Access with PhoneGap + upload with Apigee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Camera Example</title> | |
<script src="cordova.js"></script> | |
<script src="https://apigee.com/usergrid/sdk/usergrid.0.10.5.js"></script> | |
<script type="text/javascript"> | |
document.addEventListener("deviceready", onDeviceReady, false); | |
function onDeviceReady() { | |
takePhoto(); | |
} | |
// use an existing photo from the library: | |
useExistingPhoto: function(e) { | |
this.capture(Camera.PictureSourceType.SAVEDPHOTOALBUM); | |
}; | |
// take a new photo: | |
takePhoto: function(e) { | |
this.capture(Camera.PictureSourceType.CAMERA); | |
}; | |
// capture either new or existing photo: | |
capture: function(sourceType) { | |
navigator.camera.getPicture(this.onCaptureSuccess, this.onCaptureFail, { | |
destinationType: Camera.DestinationType.FILE_URI, | |
sourceType: sourceType, | |
correctOrientation: true | |
}); | |
}; | |
// if photo is captured successfully, then upload to server: | |
onCaptureSuccess: function(imageURI) { | |
var fail, ft, options, params, win; | |
// callback for when the photo has been successfully uploaded: | |
success: function(response) { | |
alert("Your photo has been uploaded!"); | |
}; | |
// callback if the photo fails to upload successfully. | |
fail: function(error) { | |
alert("An error has occurred: Code = " + error.code); | |
}; | |
// Create an asset | |
client.createEntity( | |
{"type":"asset", | |
"photographer":"Matt", | |
"owner":"364c1e7b-0fed-11e2-9c25-12313d1ac481", | |
"path":imageURI} | |
, function (err, asset) { | |
if (err) { | |
alert("could not create asset"); | |
} else { | |
alert("asset created"); | |
options = new FileUploadOptions(); | |
// parameter name of file: | |
options.fileKey = "my_image"; | |
// name of the file: | |
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); | |
// mime type: | |
options.mimeType = "application/octet-stream"; | |
ft = new FileTransfer(); | |
url = 'http://api.usergrid.com/tim/sandbox/assets/'+asset.get('uuid')+'/data' | |
console.log(url); | |
ft.upload(imageURI, url, success, fail, options); | |
}; | |
} | |
}); | |
// there was an error capturing the photo: | |
onCaptureFail: function(message) { | |
alert("Failed because: " + message); | |
}; | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment