Grabbing Image Paths For All Images in Your App’s Local “/Documents” Directory Using Titanium SDK

This is a quick example of how to grab local images that were earlier stored as PNG’s in your app’s /Documents folder and store their paths inside an array that can later be used to populate a View like CoverFlow, etc. Note: for CoverFlow, you’ll want to first resize your images down to a smaller widthxheight or CoverFlow will crash your app. I’m using the CommonJS modules approach to app structure. When testing in the Simulator, you can find your App’s /Documents folder here: /Users/your_User_Name/Library/Application Support/iPhone Simulator/5.0/Applications/FF203495C-R93475-466B-9103-EEEF0E9687B9/Documents/. A quick way to add some sample images to your Simulator’s /Documents folder, open Safari on the Simulator, find an image, Tap & Hold until the dialog comes up asking you to “Save Image”.

function SecondView() {
	
	var self = Ti.UI.createView();	
	var imgs = [];

	/* 
	 * 
	 * The FILE system API is not fully documented on Appcelerator's website. 
	 * For a more complete list of available methods for Ti.Filesystem check the Kitchen Sink's filesystem.js 
	 * 
	 * */
    				
	function loadData() {	
						
		var dir = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory);
		var fdir = dir.getDirectoryListing();
		var numPhotos = fdir.length || 1;
		 //Documents folder is empty on 1st load post install, 
		 //so unless there's a default value, you get a blank screen... 	
		
		for(var a=0; a < numPhotos; a++ ) {
		
			var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,fdir[a]);
			
			if(f.exists())
			{
				var fr = f.read();		
			
				if(fr.mimeType === 'image/png'){
					Ti.API.info('photo.nativePath: ' + f.nativePath);
					imgs.push(f.nativePath); //imgs[a] = f.nativePath;				
				}			
			}						
		}
	}

	loadData();
	
	return self;
}

module.exports = SecondView;

Advertisements

One thought on “Grabbing Image Paths For All Images in Your App’s Local “/Documents” Directory Using Titanium SDK

  1. how to get all the photo gallery images? Titanium.Filesystem.applicationDataDirectory only returns the images saved in this application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s