Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1510] Android: Add support for saveToPhotoGallery (Titanium.Media.saveToPhotoGallery)

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2014-05-28T16:04:52.000+0000
Affected Version/sRelease 1.7.0
Fix Version/s2014 Sprint 08, 2014 Sprint 08 SDK, Release 3.3.0
ComponentsAndroid
Labelsmodule_media, parity, qe-testadded, tbs-1.9.0
ReporterBill Dawson
AssigneeVishal Duggal
Created2011-04-15T02:54:47.000+0000
Updated2014-05-28T16:07:34.000+0000

Description

Currently only a log warning that it's not supported.

The code sample below demonstrates the feature required. Works fine on iphone.

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

var win1 = Titanium.UI.createWindow({  

    backgroundColor:'#fff'
});

var image_view = Ti.UI.createImageView({
		image: 'appcelerator.png',
		height: 400,
		width: 320
	});

 var button2 =  Ti.UI.createButton({
 	top:90,
 	title:'save to gallery',
 	height:40
 	
 });	
 
 win1.add(button2);
	button2.addEventListener('click', function(e){	
		win1.add(image_view);
		var img = image_view.toBlob();
		Titanium.Media.saveToPhotoGallery(img,{
			"success": function(e){
				alert('Saved to your camera roll.');	
			},
			"error": function(e){
				alert(e.error);
			}
		});			
	});
	
win1.open();

Comments

  1. Anirudh Nagesh 2011-07-06

    Associated helpdesk ticket:

    http://support-admin.appcelerator.com/display/APP-612766 The image is saved on the systems' gallery and not on the filesystem of the app.
  2. Aleksey Gureiev 2012-11-14

    I will be backporting the fix that's on the 3_0_X branch for 2_1_X in some capacity. Anyone's interested in push request for this one?
  3. Aleksey Gureiev 2012-11-15

    Here you go: https://github.com/appcelerator/titanium_mobile/pull/3427
  4. Ygor Lemos 2013-09-12

    This is still happening on Ti SDK 3.1.2.GA Tested on several devices (Android 4.0, 4.1, 4.2 and 4.3) PhoneGap solved this as per: http://simonmacdonald.blogspot.ca/2012/07/change-to-camera-code-in-phonegap-190.html I think this shouldn't be marked as trivial.
  5. Ygor Lemos 2013-09-27

    Any news on this one? Fix Version?
  6. Vishal Duggal 2014-04-12

    Test Code
       var win1 = Titanium.UI.createWindow({
           backgroundColor : '#fff',
           exitOnClose:true
       });
         
       var saveToGallery = false;
        
       var hasCamera = Ti.Media.isCameraSupported;
        
       if(hasCamera) {
           var hasBackfacingCamera = hasCamera;
           var hasFrontfacingCamera = hasCamera;
           var front = false;
           var availableCameras = Ti.Media.availableCameras;
            
           hasBackfacingCamera = (availableCameras.indexOf(Ti.Media.CAMERA_REAR) >= 0);
           hasFrontfacingCamera = (availableCameras.indexOf(Ti.Media.CAMERA_FRONT) >= 0);
        
           var b1 = Ti.UI.createButton({
               title:'Save To Gallery',
               left:0,
               top:0,
               color:'red',
               width:'50%',
               height:'10%',
               backgroundColor:'black',
           }) ;
            
           var b2 = Ti.UI.createButton({
               title:'View Gallery',
               right:0,
               top:0,
               width:'50%',
               height:'10%',
               backgroundColor:'black',
               color:'white'
           });
            
           win1.add(b1);
           win1.add(b2);
            
           var container = Ti.UI.createView({
               top:'10%',
               height:'80%'
           });
            
           var img = Ti.UI.createImageView({
           });
            
           var saveButton = Ti.UI.createButton({
               title:'SAVE',
               backgroundColor:'black',
               color:'white',
               visible:false
           });
            
           container.add(img);
           container.add(saveButton);
           win1.add(container);
            
            
           var b3 = Ti.UI.createButton({
               title:'Native Camera',
               top:'90%',
               height:'10%',
               left:0,
               width:'50%',
               backgroundColor:'black',
               color:'white'
           });
           win1.add(b3);
            
           var b4 = Ti.UI.createButton({
               title:'Camera Overlay',
               top:'90%',
               height:'10%',
               right:0,
               width:'50%',
               backgroundColor:'black',
               color:'white'
           });
            
           win1.add(b4);
            
           var overlayObj = Ti.UI.createView({});
           var picBtn = Ti.UI.createButton({
               title:'Snap Picture',
               top:'90%',
               height:'10%',
               left:0,
               width:'50%',
               backgroundColor:'black',
               color:'white'
           });
           overlayObj.add(picBtn);
           picBtn.addEventListener('click',function(){
               Ti.Media.takePicture();
           });
           if(hasFrontfacingCamera && hasBackfacingCamera) {
               var swapBtn = Ti.UI.createButton({
                   title:'Switch Camera',
                   top:'90%',
                   height:'10%',
                   right:0,
                   width:'50%',
                   backgroundColor:'black',
                   color:'white'
               });
                
               overlayObj.add(swapBtn);
               swapBtn.addEventListener('click',function(){
                   if(front == true) {
                       Ti.Media.switchCamera(Ti.Media.CAMERA_REAR);
                   } else {
                       Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
                   }
                   front = !front; 
               });
           } else {
               //Adjust Button
               picBtn.width = Ti.UI.FILL;
           }
            
            
           saveButton.addEventListener('click',function(e){
               if(img.image != undefined) {
                   Ti.Media.saveToPhotoGallery(img.image,{
                       success:function(){
                           alert('Saved. Check In Gallery');
                       },
                       error:function(e){
                           var msg = 'Code: '+e.code+' Msg: '+e.error;
                           alert(msg);
                       }
                   });
                    
               } else {
                   Ti.API.info('NO IMAGE ON img to save to photo gallery');
               }
           });
            
           b1.addEventListener('click',function(){
               saveToGallery = !saveToGallery;
               if(saveToGallery == true) {
                   b1.color = 'green';
               } else {
                   b1.color = 'red';
               }
           });
            
            
           b2.addEventListener('click',function(){
               Ti.Media.openPhotoGallery({});
           });
            
            
           b3.addEventListener('click',function(){
               fireUpTheCamera(saveToGallery,false, false);
           });
            
           b4.addEventListener('click',function(){
               if(hasBackfacingCamera && hasFrontfacingCamera) {
                   //Alwways start with back camera
                   front = false;
               } else {
                   //Start with what ever camera is available
                   front = hasFrontfacingCamera;
               }
               fireUpTheCamera(saveToGallery,true, front);
           });
            
           function fireUpTheCamera(save, hasoverlay, front) {
               var options = {
                   success : function(e) {
                       Ti.API.info('GOT SUCCESS CALLBACK');
                       var imgWid = e.media.width;
                       var imgHeight = e.media.height;
                       var mSize = container.size;
                       var mW = mSize.width;
                       var mH = mSize.height;
                        
                       var wS = imgWid/mW;
                       var hS = imgHeight/mH;
                       var tS = (wS < hS) ? wS : hS;
                        
                       var dstWid = imgWid/tS;
                       var dstHeight = imgHeight/ tS;
                        
                       Ti.API.info(imgWid+' '+imgHeight+' '+mW+' '+mH+' '+wS+' '+hS+' '+tS+' '+dstWid+' '+dstHeight);
                       var resize = e.media.imageAsResized(dstWid,dstHeight);
                       img.image = resize;
                       saveButton.visible = true;
                   },
                   cancel : function() {
                       // create alert
                       var a = Titanium.UI.createAlertDialog({
                           title : 'Camera',
                           message: 'Action cancelled by user'
                       });
                       a.show();     
                   },
                   error : function(error) {
                       // create alert
                       var a = Titanium.UI.createAlertDialog({
                           title : 'Camera'
                       });
              
                       // set message
                       var msg = 'Unexpected error: ' + error.code;
                       if(error.error != undefined) {
                           msg = msg + ' Message: '+error.error;
                       }
                       a.setMessage(msg);
                       a.show();
                   },
                   saveToPhotoGallery : save,
                   mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO],
                   autohide: true
               };
               if(hasoverlay) {
                   options.overlay = overlayObj;
               }
               if(front) {
               	options.whichCamera = Ti.Media.CAMERA_FRONT;
               }
               Titanium.Media.showCamera(options);
           }
       }else {
           var label = Ti.UI.createLabel({
               text:'NO CAMERAS ON DEVICE',
               color:'red'
           });
           win1.add(label);
       }
        
       win1.open();
       
  7. Vishal Duggal 2014-04-14

    Pull pending https://github.com/appcelerator/titanium_mobile/pull/5597

JSON Source