Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-26602

Android: Ti.Media.takePicture() will wrongly assign mp4 extension to image file if camera is configured for MEDIA_TYPE_VIDEO

    Details

      Description

      Hi there. Using this code:

      Ti.Media.showCamera({
            autohide : false,
            showControls : false,
            overlay : cameraControls.getView(),
            saveToPhotoGallery : false,
            videoMaximumDuration : 60 * 1000,
            mediaTypes : [ Titanium.Media.MEDIA_TYPE_PHOTO, Titanium.Media.MEDIA_TYPE_VIDEO ],
            success : function(e) {
              Ti.API.info(' e.media.mimeType: ' + e.media.mimeType);
            },
            error : function(e) {
              Ti.API.error(JSON.stringify(e));
            },
            cancel : function() {
              Ti.API.info('Action cancelled');
            }
          });
      

      And calling Ti.Media.takePicture() from "cameraControls" controller, outputs "video/mp4" in the console, i.e., I've opened the camera for both, taking a photo or a video, but if I take a photo, the callback returns a video mimeType with .mp4 extension.

      I've fixed this by commenting an if block in TiCameraActivity.java (package ti.modules.titanium.media):

      private static File writeToFile(byte[] data, boolean saveToGallery) throws Throwable
      	{
      		try {
      			File imageFile = null;
      			if (saveToGallery) {
      				imageFile = MediaModule.createGalleryImageFile();
      			} else {
      				// Save the picture in the internal data directory so it is private to this application.
      				String extension = ".jpg";
      				// Rodrigo Farfán (phobeous@gmail.com):
      				// This has no sense. The only point where this method is called is in jpegCallback and
      				// video recording creates his own file. So this is absolutely wrong
      				// Let's comment out next if block
      				/*
      				if (mediaType == MEDIA_TYPE_VIDEO) {
      					extension = ".mp4";
      				}
      				*/
      				imageFile = TiFileFactory.createDataFile("tia", extension);
      			}
       
      			FileOutputStream imageOut = new FileOutputStream(imageFile);
      			imageOut.write(data);
      			imageOut.close();
       
      			if (saveToGallery) {
      				Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      				Uri contentUri = Uri.fromFile(imageFile);
      				mediaScanIntent.setData(contentUri);
      				Activity activity = TiApplication.getAppCurrentActivity();
      				activity.sendBroadcast(mediaScanIntent);
      			}
      			return imageFile;
       
      		} catch (Throwable t) {
      			throw t;
      		}
      	}
      

      Please, review this and consider adding the fix to master branch for future releases.

      Best regards

        Attachments

          Activity

            People

            • Assignee:
              jquick Joshua Quick
              Reporter:
              rfarfan Rodrigo Farfán
              Reviewer:
              Gary Mathews
              Tester:
              Sohail Saddique
            • Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-6736
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Integration