Get images from web > save to phone?

You must Login before you can answer or comment on any questions.

Hi, I'm busy with developing an app for mobile. For Android and iOS. And I have a question. I'm loading images with JSON from my website. Then I'll show the images in a slide-show. Is it possible to save the images to the phone somewhere after loading? So the images are also offline available. Also a good thing will be, that the user of the app, doesn't have to download the images gallery every time (because internet speed can suck). The app will be faster after the first download. How can I do that? I couldn't find an example like that in KitchenSink.

4 Answers

Yes, I saw that one. But that's just a file download? Or does it really save the file after download to somewhere? If so; where?

You simply create a httpClient and perform a GET request, Inside the onload Event you can Access this.responseData. It will contain the binary Image, now all you need is to write it to a File using Ti.Filesystem namespace.

It basically Downloads an Image to the Memory.

Here is a function that will take a remote png file (that is what I use), check to see if it has already been downloaded by the app, if so, then loads from the phone file system. If not, then it loads the remote image and saves to the phone file system... Hope this helps.

exports.RemoteImage = function(a){
        a = a || {};
        var md5;
        var needsToSave = false;
        var savedFile;
        if(a.image){
            md5 = Ti.Utils.md5HexDigest(a.image)+'.png';
            savedFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,md5);
            if(savedFile.exists()){
                a.image = savedFile;
            } else {
                needsToSave = true;
            }
        }
 
        var image = Ti.UI.createImageView(a);
 
        if(needsToSave == true){
            function saveImage(e){
                image.removeEventListener('load',saveImage);
                savedFile.write(
                    Ti.UI.createImageView({image:image.image,width:Ti.UI.SIZE,height:Ti.UI.SIZE}).toImage()
                );
            };
            image.addEventListener('load',saveImage);
        }
 
        return image;
 
    };

Your Answer

Think you can help? Login to answer this question!