Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created September 22, 2011 19:01
Show Gist options
  • Save jonalter/1235684 to your computer and use it in GitHub Desktop.
Save jonalter/1235684 to your computer and use it in GitHub Desktop.
iOS/Android: read the bytes of a file
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
win.open();
var button = Ti.UI.createButton({
title : "Click me!",
height : 50,
width : 200,
top : 100
});
win.add(button);
button.addEventListener('click', function(e) {
var resourceFileStream = Ti.Filesystem.openStream(Ti.Filesystem.MODE_READ, Ti.Filesystem.resourcesDirectory, 'KS_nav_ui.png');
var buffer = Titanium.createBuffer({
length : 32
});
// this will show the decimal representation of the first byte of the file
// resourceFileStream.read(buffer);
// Ti.API.info('buffer: ' + buffer[0]);
// this will show the the bytes of the whole file in decimal and hex
while(( size = resourceFileStream.read(buffer)) > -1) {
Ti.API.info('===============================================');
for (var i=0, j = buffer.length; i < j; i++) {
Ti.API.info("Decimal: "+buffer[i]);
Ti.API.info("Hex: "+buffer[i].toString(16));
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment