Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (double)
Viewing all articles
Browse latest Browse all 10

Camera video file double size problem

$
0
0

I'm testing this on iPhone/iPad devices using 2.1.3.GA and 3.0.0.GA sdk versions.

  • Application: I'm recording HQ video files using the iPhone camera.
  • Problem: If I record a 100mb video file, the application shows 200mb usage on data/documents storage. If I delete the file, the application shows now 100mb on data/documents and it's impossible to release this space. It seems that there's a temporary... or cache .. or whatever file that is not being deleted after saving the video file to the "disk".
  • Workaround: Uninstall and reinstall the application to release space ... which is annoying.

I will post some code to reproduce this behavior. It basically opens the camera with one button, lists all files from all the accessible folders with the second button, and deletes all saved video files with the third one.

Steps to reproduce this problem :

  1. 1 - Install the application to a device.

  2. 2 - Check app size (Settings->General->Usage and look for the app... it should be around 5Mb and 0Mb on data/documents).

  3. 3 - Open the app, press the "camera" button and record 1 minute of video or so.

  4. 4 - Press the "list files console" button and check the console output on Xcode to check the new file size. (you can check this on iTunes sharing files also as the app is set to share the files).

  5. 5 - Check app size (repeat step 2) ... you will see that it's: [video size * 2 + 5mb].

  6. 6 - Go back to the app and press the "delete all files" button.

  7. 7 - Repeat steps 4 and 5 to check sizes. You will see that the file is no longer listed but the app size is [video size + 5mb]

So.. imagine this situation with a 5Gb video file... it will take 10Gb when saved and release only half of the space when deleted.

Paste this code on app.js to reproduce it and let me know if I'm doing something wrong please!! this is driving me nuts!:

// open a single window
var win = Ti.UI.createWindow({
    backgroundColor:'red'
});
 
var btn = Ti.UI.createButton({
    title: 'camera',
   top: 10,
   width: 300,
   height: 50   
});
btn.addEventListener('click',function(e){
    showCam();
});
win.add(btn);
 
var btn2 = Ti.UI.createButton({
    title: 'list file console',
    top: 70,
    height:50,
    width:300
})
 
btn2.addEventListener('click',function(){
    getSavedFiles();
});
win.add(btn2);
 
 
var btn3 = Ti.UI.createButton({
    title: 'delete all files',
    top: 140,
    height:50,
    width:300
})
 
btn3.addEventListener('click',function(){
    deleteAll();
});
win.add(btn3);
 
 
win.open();
 
 
function showCam(){
    Titanium.Media.showCamera({
 
        success:function(event)
        {
 
            saveToDisk('VIDEO',event.media, true);  
            Ti.Media.hideCamera();  
        },
        cancel:function()
        {
        },
        error:function(error)
        {
 
            var a = Titanium.UI.createAlertDialog({title:L('error_message_title')});
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage(L('run_test_on_device'));
            }
            else
            {
                a.setMessage('Unexpected error: ' + error.code);
            }
            a.show();
        },
        showControls:true,  
        mediaTypes: Ti.Media.MEDIA_TYPE_VIDEO,
        videoQuality: Ti.Media.QUALITY_HIGH,
        cameraFlashMode: Ti.Media.CAMERA_FLASH_OFF,
        videoMaximumDuration:86400000,  
        allowEditing: false, 
        autohide: false     
    });
}
 
 
var saveToDisk= function(type,data,isSave){
    var filename = createFilename(type);
 
    var mediaFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
    mediaFile.write(data);      
}
 
var createFilename = function(type){
    // create name based on timestamp
 
    var today = new Date();
    var sToday = today.getFullYear().toString();
    var the_month;
    if (today.getMonth()+1 < 10){
        the_month = '0' + (today.getMonth()+1).toString();
    }else{
        the_month = (today.getMonth()+1).toString();
    }
    sToday += the_month;
    var the_day;
    if (today.getDate() < 10){
        the_day = '0' + today.getDate().toString();
    }else{
        the_day = today.getDate().toString();
    }
    sToday += the_day;
    var the_hour;
    if (today.getHours() < 10){
        the_hour = '0' + today.getHours().toString();
    }else{
        the_hour = today.getHours().toString();
    }
    sToday += the_hour;
    var the_minutes;
    if (today.getMinutes() < 10){
        the_minutes = '0' + today.getMinutes().toString();
    }else{
        the_minutes = today.getMinutes().toString();
    }
    sToday += the_minutes;
    var the_seconds;
    if (today.getSeconds() < 10){
        the_seconds = '0' + today.getSeconds().toString();
    }else{
        the_seconds = today.getSeconds().toString();
    }
    sToday += the_seconds;
    var ext = '';
    switch(type){
        case 'PHOTO':
            ext = 'jpg';
            break;
        case 'VIDEO':
            ext = 'mov';
            break;
        case 'AUDIO':
            ext = 'wav';
            break;
    }
    sToday += '.' + ext;
    return sToday;
}
var getSavedFiles = function(){
    // List all folders trying to find where the space is.
 
    // Cache folder
    var dirFullPath = Ti.Filesystem.applicationCacheDirectory ;
    var dir = Titanium.Filesystem.getFile(dirFullPath);
    var dirItems = dir.getDirectoryListing();
 
    for ( var i=dirItems.length-1; i>=0; i-- ) {
        var itemFullPath = dirFullPath + Titanium.Filesystem.separator + dirItems[i].toString();
        Ti.API.info('File cache fodler #'+i+': '+itemFullPath);         
 
        var item = Ti.Filesystem.getFile(itemFullPath);
        var blob= item.read();
        Ti.API.info('size: '+ blob.length/1024/1024+ ' mb');
 
        item=null;
        blob=null;      
    }
 
    // resources foldes
    var dirFullPath = Ti.Filesystem.resourcesDirectory ;
    var dir = Titanium.Filesystem.getFile(dirFullPath);
    var dirItems = dir.getDirectoryListing();
 
    for ( var i=dirItems.length-1; i>=0; i-- ) {
        var itemFullPath = dirFullPath + Titanium.Filesystem.separator + dirItems[i].toString();
        Ti.API.info('File resources fodler #'+i+': '+itemFullPath); 
        var item = Ti.Filesystem.getFile(itemFullPath);
        var blob= item.read();
        Ti.API.info('size: '+ blob.length/1024/1024+ ' mb');    
        item=null;
        blob=null;                  
    }
 
    // temp folder
    var dirFullPath = Ti.Filesystem.tempDirectory ;
    var dir = Titanium.Filesystem.getFile(dirFullPath);
    var dirItems = dir.getDirectoryListing();
 
    for ( var i=dirItems.length-1; i>=0; i-- ) {
        var itemFullPath = dirFullPath + Titanium.Filesystem.separator + dirItems[i].toString();
        Ti.API.info('File temp fodler #'+i+': '+itemFullPath);
        var item = Ti.Filesystem.getFile(itemFullPath);     
        var blob= item.read();
        Ti.API.info('size: '+ blob.length/1024/1024+ ' mb');
        item=null;
        blob=null;      
    }
 
 
    // app data folder  
    var dirFullPath = Ti.Filesystem.applicationDataDirectory ;
    var dir = Titanium.Filesystem.getFile(dirFullPath);
 
    var dirItems = dir.getDirectoryListing();
    for ( var i=dirItems.length-1; i>=0; i-- ) {
        var itemFullPath = dirFullPath + Titanium.Filesystem.separator + dirItems[i].toString();
        Ti.API.info('File data fodler #'+i+': '+itemFullPath);      
        var item = Ti.Filesystem.getFile(itemFullPath);
        var blob= item.read();
        Ti.API.info('size: '+ blob.length/1024/1024+ ' mb');
 
        item=null;
        blob=null;
    }
 
    var a = Titanium.UI.createAlertDialog({title:''});
    a.setMessage('Done!');
    a.show();
}
 
 
 
var deleteAll = function() {
    var dirFullPath = Ti.Filesystem.applicationDataDirectory ;
    var dir = Titanium.Filesystem.getFile(dirFullPath);
 
    var dirItems = dir.getDirectoryListing();
    for ( var i=dirItems.length-1; i>=0; i-- ) {
        var itemFullPath = dirFullPath + Titanium.Filesystem.separator + dirItems[i].toString();
        Ti.API.info('delete file data fodler #'+i+': '+itemFullPath);       
        var item = Ti.Filesystem.getFile(itemFullPath);
        var blob= item.read();
        Ti.API.info('size: '+ blob.length/1024/1024+ ' mb');
        item.write('',false);
        item.deleteFile();      
        item=null;      
        blob=null;      
 
    }
 
 
    var a = Titanium.UI.createAlertDialog({title:''});
    a.setMessage('Done!');
    a.show();
 
 
}
Thank you for any clue!

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images