Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Save and Display Image in ImageView from FileSystem

$
0
0

I want to save an image to the file system, save the file name in a SQL DB, and then re-open the image in a different view later.

To save the image I am:

Ti.Media.showCamera({
        saveToPhotoGallery: false,
        success: function(event) {
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                var newImgView = Ti.UI.createImageView({
                  image: event.media
                });
                $.decisionImages.add(newImgView);
                var imgName = createImgName($.decisionImages.length - 1);
                createImg(event.media, someId, imgName);
            }
        }
    });
 
function createImgName(imageNum){
    return "img_" + (new Date().getTime()) + "_" + imageNum + ".png";
}
 
function createImg(image, imgName){
    var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imgName);
    f.write(image);
 
    var image = Alloy.createModel('image',{
            "image_path": imgName
    });
 
    image.save();
}
Then later on, I am reading the "image" Model out and trying to load the image file from the File System. I seem to be able to find the file, but the image is blank on the view which makes me think it's either an error with how I am writing the file or how I am reading it into the ImageView.

Code for reading:

var images = Alloy.createCollection('image');
    images.fetch();
 
    for(var i = 0; i < images.length; i++){
        var imageView = Ti.UI.createImageView({
            image: Titanium.Filesystem.applicationDataDirectory + images.at(i).get("image_path")
        });
 
        $.bottomContainer.add(imageView);
    }
Any help would be amazing! I've been looking around this Q&A site and the Wiki and feel as though I am doing exactly what they say to do!

Viewing all articles
Browse latest Browse all 8068

Trending Articles