Hi guys I've been working on the Android version of my app but have been running into issues getting the attachment section running correctly. The feature has the option to where and image can be attached via two options: 1) Take picture and save then attach to image placeholder, 2) Attach picture from gallery. Option 2 works fine but option 1 does not work. Someone please help as this is the only thing keeping me completing the app. I'm adding a good portion of the code to get the best assistance and so that anyone who would like to use it for their project can do so :).
Here is the code:
//load component dependencies var androidUploadProgress = 0; var curHeight = Titanium.Platform.displayCaps.platformHeight; var curWidth = Titanium.Platform.displayCaps.platformWidth; var font_header = curWidth * 6 / 100; var font_title = curWidth * 5 / 100; var font_sub_title = curWidth * 4 / 100; var row_height = curHeight / 10; //create component instance //create component instance var finalName = ""; var name_count = 0; var count_image = 0; var count_upload = 0; var image_value = new Array(); var image1_flag = false, image2_flag = false; var submit_img1 = Ti.UI.createImageView({ width : '20%', height : '10%', top : '3%', left : '10%', image : Ti.Filesystem.resourcesDirectory + "image/placeholder.png", backgroundDisabledColor : true, }); var submit_btn1 = Ti.UI.createButton({ width : '30%', height : Ti.UI.SIZE, top : '15%', left : '5%', title : 'Image1', //image : Ti.Filesystem.resourcesDirectory + "image/contact.png", }); var submit_img2 = Ti.UI.createImageView({ width : '20%', height : '10%', top : '3%', right : '10%', image : Ti.Filesystem.resourcesDirectory + "image/placeholder.png", backgroundDisabledColor : true, }); var submit_btn2 = Ti.UI.createButton({ width : '30%', height : Ti.UI.SIZE, top : '15%', right : '5%', title : 'Image2', //image : Ti.Filesystem.resourcesDirectory + "image/contact.png", }); var upload_btn = Ti.UI.createButton({ width : '30%', height : Ti.UI.SIZE, top : '82%', right : '5%', title : 'Upload', //image : Ti.Filesystem.resourcesDirectory + "image/contact.png", }); var SubmitForm_btn = Ti.UI.createButton({ width : '90%', height : Ti.UI.SIZE, top : '90%', right : '5%', left : '5%', title : 'Submit Form', }); var opts = { title : 'Select to Upload' }; var isAndroid = Ti.Platform.osname == 'android'; if (isAndroid) { opts.options = ['Take Pic', 'Select from Gallary']; opts.buttonNames = ['Confirm']; } else { //opts.options = ['Confirm', 'Help', 'Cancel']; } //image Uploding --Image 1 submit_btn1.addEventListener('click', function(e) { var sourceSelect1 = Ti.UI.createOptionDialog(opts); sourceSelect1.show(); sourceSelect1.addEventListener('click', function(e) { Ti.App.pic = Ti.App.Properties.getInt('pic'); if (e.index === 0) {//Camera Titanium.Media.showCamera({ success : function(e) { var image = e.media.imageAsThumbnail(300); if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { Ti.App.Properties.setInt('pic', Ti.App.pic + 1); Ti.App.pic = Ti.App.Properties.getInt('pic'); var picFileName = 'pic' + Ti.App.pic + '.png'; submit_img1.image = image; image1_flag = true; image_value[0] = e.media; } }, error : function(error) { if (error.code == Titanium.Media.NO_CAMERA) { alert(L('no_camera')); } }, saveToPhotoGallery : true, allowEditing : true, mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] }); } else if (e.index === 1) {//PhotoCallery Ti.Media.openPhotoGallery({ success : function(e) { var image = e.media.imageAsThumbnail(300); if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { Ti.App.Properties.setInt('pic', Ti.App.pic + 1); Ti.App.pic = Ti.App.Properties.getInt('pic'); var picFileName = 'pic' + Ti.App.pic + '.png'; submit_img1.image = image; image1_flag = true; image_value[0] = e.media; } }, error : function(error) { alert(error.code) }, allowEditing : false, mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] }); } }); }); //image Uploding --Image 2 submit_btn2.addEventListener('click', function(e) { var sourceSelect2 = Ti.UI.createOptionDialog(opts); sourceSelect2.show(); sourceSelect2.addEventListener('click', function(e) { Ti.App.pic = Ti.App.Properties.getInt('pic'); if (e.index === 0) {//Camera Titanium.Media.showCamera({ success : function(e) { var image = e.media.imageAsThumbnail(300); if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { Ti.App.Properties.setInt('pic', Ti.App.pic + 1); Ti.App.pic = Ti.App.Properties.getInt('pic'); var picFileName = 'pic' + Ti.App.pic + '.png'; submit_img2.image = image; image2_flag = true; image_value[1] = e.media; } }, error : function(error) { if (error.code == Titanium.Media.NO_CAMERA) { alert(L('no_camera')); } }, saveToPhotoGallery : true, allowEditing : true, mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] }); } else if (e.index === 1) {//PhotoCallery Ti.Media.openPhotoGallery({ success : function(e) { var image = e.media.imageAsThumbnail(300); if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { Ti.App.Properties.setInt('pic', Ti.App.pic + 1); Ti.App.pic = Ti.App.Properties.getInt('pic'); var picFileName = 'pic' + Ti.App.pic + '.png'; submit_img2.image = image; image2_flag = true; image_value[1] = e.media; } }, error : function(error) { alert(error.code) }, allowEditing : false, mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] }); } }); }); //progress bar var pb = Titanium.UI.createProgressBar({ top : '50%', width : '90%', left : '5%', height : 'auto', min : 0, max : 80, value : 0, color : 'black', message : 'Image Uploading....', font : { fontSize : font_header, fontWeight : 'bold' }, style : Titanium.UI.iPhone.ProgressBarStyle.PLAIN, visible : false }); function UploadPhotoToServer(args, media) { if (Titanium.Network.online == true) { var xhr = Titanium.Network.createHTTPClient(); xhr.onerror = function(e) { Ti.API.info('IN ERROR ' + e.error); alert('Sorry, we could not upload your photo! Please try again.'); }; xhr.onload = function() { //alert(this.responseText); if (name_count == 0) { var res = this.responseText; finalName += res; name_count++; Ti.API.info('Final Name' + finalName); } else { var res = this.responseText; finalName += "," + res; name_count++; //finalName =String(finalName ,+ this.responseText()); name_count++; Ti.API.info('Final Name' + finalName); } if (count_upload <= 70) { count_upload += 10; pb.setValue(count_upload); if (count_upload == 80) { //alert("Image Uploaded") pb.visible = false; pb.hide(); } } else { pb.visible = false; pb.hide(); }; }; xhr.onsendstream = function(e) { //Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress); }; xhr.open('POST', 'http://www.url.com/ajax2.php?Action=upload_image'); if (args == 1) { //pb.setValue(10); xhr.send({ userfile : media, }); }; if (args == 2) { //pb.setValue(20); xhr.send({ userfile : media, }); }; } else { //activityIndicator.hide(); alert('You must have a valid Internet connection in order to upload this photo.'); } } self.addEventListener('android:back', function() { self.close(); Window = require('ui/handheld/android/Application_dependent2'); new Window().open(); }); scroll.add(Id_lbl); scroll.add(submit_btn1); scroll.add(submit_img1); scroll.add(submit_btn2); scroll.add(submit_img2); var style; if (Ti.Platform.name === 'iPhone OS') { style = Ti.UI.iPhone.ActivityIndicatorStyle.DARK; } else { style = Ti.UI.ActivityIndicatorStyle.DARK; } activityIndicator = Ti.UI.createActivityIndicator({ color : 'white', font : { fontFamily : 'Helvetica Neue', fontSize : font_header, fontWeight : 'bold' }, message : ' Loading...', style : style, top : '35%', left : "40%", height : '10%', width : Ti.UI.SIZE }); content_view.add(activityIndicator); content_view.add(scroll); win.add(content_view); win.add(pb); pb.hide(); self.add(win); return self; } //make constructor function the public component interface module.exports = Application_Attachment;