Created
September 16, 2012 18:01
-
-
Save timoa/3733505 to your computer and use it in GitHub Desktop.
Save file iOS and Android with Appcelerator Titanium Mobile SDK (http://timoa.com/en/2012/07/appcelerator-nativepath-on-ios-and-android/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OS | |
if(Ti.Platform.osname === 'iPhone' || Ti.Platform.osname === 'iPad') { | |
var ios = true; | |
} | |
/** | |
* Save file function | |
* | |
* @file: Binary file (Blob) | |
* @filename: Name of file (String) | |
* | |
*/ | |
function saveFile(_args) { | |
// Test if External Storage (Android only) | |
if(Ti.Filesystem.isExternalStoragePresent()){ | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, _args.filename); | |
} | |
// No SD or iOS | |
else { | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, _args.filename); | |
} | |
// Save file | |
file.write(_args.file); | |
// Debug: Test if file exist now | |
if(file.exists) { | |
Ti.API.info('[saveFile] Saved: YES! (' + file.nativePath + ')'); | |
} else { | |
Ti.API.info('[saveFile] Saved: NO!'); | |
} | |
// Return full path of file | |
if(ios && Ti.version <= '1.8.2') { | |
var iosPath = Ti.Filesystem.applicationDataDirectory + _args.filename; | |
iosPath = iosPath.replace('file://','app://'); | |
return iosPath; | |
} | |
else { | |
return file.nativePath; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To use, simply call the function in this way : | |
var pathToImage = saveFile({ | |
file: ***blob***, | |
filename: 'image.png' | |
}); |
Thanks for this code snippet! It's really helpful =)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for posting this :)