Created
October 3, 2017 13:00
-
-
Save romuloinnocencio/7d88cf440674318199bceb36040af301 to your computer and use it in GitHub Desktop.
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
document.addEventListener("deviceready", onDeviceReady, false); | |
function onDeviceReady(){ | |
var filePath; | |
var ofileEntry; | |
function returnCodAlun(){ | |
var iduser = +localStorage.getItem('scbabyc_12_user'); | |
var ls = JSON.parse(localStorage.getItem('scbabyc_childs')); | |
return +ls[iduser].codAlun; | |
}; | |
function setOptions( srcType ){ | |
var options = { | |
sourceType: srcType, | |
quality: 50, | |
destinationType: Camera.DestinationType.FILE_URI, | |
encodingType: Camera.EncodingType.JPG, | |
mediaType: Camera.MediaType.PICTURE, | |
allowEdit: true, | |
correctOrientation: true | |
} | |
filePath = options.destinationType; | |
return options; | |
}; | |
function onErrorResolveUrl( e ){ | |
console.error('onErrorResolveUrl'); | |
}; | |
function onErrorCreateFile( e ){ | |
console.error('onErrorCreateFile'); | |
}; | |
function createNewFileEntry( imgUri ){ | |
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function success( dirEntry ){ | |
dirEntry.getFile('tempFile.jpg',{create: true, exclusive: false },function( fileEntry ){ | |
ofileEntry = fileEntry; | |
writeFile(fileEntry); | |
}, onErrorCreateFile); | |
}, onErrorResolveUrl); | |
}; | |
function writeFile( fileEntry, dataObj ){ | |
// Create a FileWriter object for our FileEntry (log.txt). | |
fileEntry.createWriter(function( fileWriter){ | |
fileWriter.onwriteend = function(){ | |
readFile(fileEntry); | |
}; | |
fileWriter.onerror = function( e ){ | |
console.log("Failed file write: " + e.toString()); | |
}; | |
if( !dataObj ) | |
dataObj = new Blob(['some file data'], { type: 'text/plain' }); | |
fileWriter.write(dataObj); | |
}); | |
} | |
//pegar foto dos álbuns | |
function openFilePicker(){ | |
var options = setOptions(Camera.PictureSourceType.PHOTOLIBRARY); | |
navigator.camera.getPicture(function cameraSuccess( imageUri ){ | |
displayImage(imageUri); | |
createNewFileEntry(imageUri); | |
},function cameraError( error ){ | |
console.debug("Unable to obtain picture: " + error, "app"); | |
}, options); | |
}; | |
//tirar foto com a câmerda | |
function openCamera(){ | |
var options = setOptions(Camera.PictureSourceType.CAMERA); | |
navigator.camera.getPicture(function cameraSuccess( imageUri ){ | |
displayImage(imageUri); | |
createNewFileEntry(imageUri); | |
}, function cameraError(error) { | |
console.debug("Unable to obtain picture: " + error, "app"); | |
}, options); | |
}; | |
// | |
function uploadPhoto(){ | |
var serviceURL = encodeURI('minhaurl/upload.php'); | |
var fileURL = ofileEntry.nativeURL; | |
var options = new FileUploadOptions(); | |
options.fileKey = 'file'; | |
options.fileName = 'receita'+ Math.floor(new Date/1000); | |
options.mimeType = 'image/jpg'; | |
options.chunkedMode = false; | |
options.headers = { | |
Authorization : smbaby_token, | |
}; | |
var params = new Object(); | |
params.co00Apme = "2"; | |
params.codAlun = returnCodAlun(); | |
options.params = params; | |
var ft = new FileTransfer(); | |
ft.upload(fileURL,serviceURL,function( result ){ | |
alert(JSON.stringify(result)); | |
//$(document).trigger('submitRecipe-done',[true]); | |
}, function(error){ | |
alert(JSON.stringify(error)); | |
//$(document).trigger('submitRecipe-done',[false]); | |
}, options, true); | |
} | |
//mostra o preview | |
function displayImage( imgUri ) { | |
$('#js-previewFile').attr('src',imgUri).parent().addClass('is-visible'); | |
} | |
$(document).on('submitRecipe',function(){ | |
uploadPhoto(); | |
}); | |
$('#js-openFilePicker').on('click',function(){ | |
openFilePicker(); | |
}); | |
$('#js-openCamera').on('click',function(){ | |
openCamera(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment