Eu peguei o exemplo da documentação oficial e tudo mais... e mais os posts de vocês aqui, mas, quando eu seleciono uma imagem o Álbum ou Câmera, o Cordova cria na pasta (cordova.file.externalDataDirectory) um "tempFile.jpg" apenas com 14Bytes, sendo que a imagem original tem 200KB e a qualidade foi configurada para ter 50% pelo menos.
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();
});
}