Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-5312] Android: Write remote images to disk fails

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionCannot Reproduce
Resolution Date2012-02-10T00:31:17.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsfilesystem, remote
ReporterMads Moller
AssigneeHieu Pham
Created2011-09-18T23:37:50.000+0000
Updated2012-02-10T00:31:17.000+0000

Description

When download an image gallery and save it to the phones filesystem, problems occur. It works great with 30-50 images at the time. But a large amount of images resolves in some of the images are not saved to disk. The skipped images are random each time I test this. Sometimes it is only a few that are left out, other times its 20 images out of 150. Its the same behaviour on iOS and Android. Here is my json loader of the remote images:
 
xhr.onload = function() {
    try {
        var items = eval('('+this.responseText+')');
        for (var c=1;c<items.length;c++)
        {           
            var id = items[c].id;
            var imageLarge = items[c].imageLarge;
            var imageSmall = items[c].imageSmall;
            var imageMedium = items[c].imageMedium; 
 
            //image related content
            var imageArr = [imageLarge, imageSmall, imageMedium];
            var imageNameArr = ['imageLarge_', 'thumb_', 'thumb_'];
            var imageNativePath = [];
 
            for (var i=0; i < imageArr.length; i++) {
                var filename = xtractFile(imageArr[i]);
                var imageName = '' + imageNameArr[i] + id + filename.extension;
 
                //SAVE REMOTE FILE NOW !
                //get_remote_file( filename, url, debug, progress, override)
                getRemoteFile('gallery/'+imageName, imageArr[i], function(fileobj){ Ti.API.info(fileobj) }, null, true );
            };
 
        //all done 
        showContent();
    }
        catch(e) {
            //something goes wrong - showcontent
            trace("CATCH ERROR: " + e);
        }
    };
    xhr.onerror = function() {
        trace("ONERROR: " + e);
    };
    xhr.open("GET",rssFeed);
    xhr.send(); 
};
The below is my save to disk function. It works great when the app is handling 50 images at the time, but once its higher, some of the images are not stored, even though this below function is called.
 
function getRemoteFile(filename, url, fn_end, fn_progress, override ) {
    var file_obj = {file:filename, url:url, path: null};
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
 
    if ( file.exists() ) {
        file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
        if(override) {
            file.deleteFile();
        } else {
            return;
        }     
    }
    if ( Titanium.Network.online ) {
        var c = Titanium.Network.createHTTPClient();
        c.setTimeout(10000);
        c.onload = function()
        {
            if (c.status == 200 ) {
                var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
 
                //write file to disk
                f.write(this.responseData);
 
                if(f.size == 0) {
                    //size 0 - start over
                    trace("ERROR: image write, try again");
                    getRemoteFile(filename, url, fn_end, fn_progress, override )
                }
 
                file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
            } else {
                file_obj.error = 'file not found'; // to set some errors codes
            }
        };
        c.ondatastream = function(e)
        {
            if ( fn_progress ) fn_progress(e.progress);
        };
        c.error = function(e)
        {
 
            file_obj.error = e.error;
            fn_end(file_obj);
            trace("error: " + e);
        };
        c.open('GET',url);
        c.send();           
    } else {
        file_obj.error = 'no internet';
        fn_end(file_obj);
    }  
};
json feed: http://cph.napp.dk/index.php?option=com_phocagallery&view=napp&format=json There are no errors or exceptions I can paste in, due to the fact that Titanium does not make "errors" it just skips a lot of files. Yes, it has nothing to do with the server. I have tried without the "write remote to disk functionality" and the gallery works great. Q&A Thread at [save 100+ remote images to disk](http://developer.appcelerator.com/question/123769/save-100-remote-images-to-disk)

Attachments

FileDateSize
TestImageApp.zip2011-09-18T23:37:50.000+00002330453

Comments

  1. Mads Moller 2011-10-06

    Hi, any new progress here? I would love a fix to this, due to my apps cannot write these images to the filesystem... cheers!
  2. Hieu Pham 2012-01-10

    I tested the app and I was unable to see any images (sometimes I saw 1) inside the scrollView. The process of downloading images was fine. I ran it about 10 times and I got nearly 300 images every time, without any missing images. After simplifying the code, it seemed that the test app did not wait for the images to be generated in /gallery folder before adding it to imageView, which resulted in incorrect path for the image file. I confirmed this by separating the process, i.e download only on the first run, check to make sure image is there, then load it with imageView in the next run, and it works. Unable to reproduce the behavior described in this ticket.
  3. Thomas Huelbert 2012-02-09

    closing as we are not able to reproduce using master (1.9.0 Feb 9 2012 09:46 rd47ce8a4)

JSON Source