How to get a Mobile TiBlob as string
I am using Titanium Developer 1.2.1, iPhone sdk 4.1, Android 1.6 sdk, Titanium sdk 1.4.1.1.
I need to be able to see the bytes in a TiBlob as a string. I'm using the mobile version of appcelerator.
I have tried this but it doesn't work inside the success function of openMediaGallery:
var image = event.media;
var tempFile = Titanium.Filesystem.createTempFile();
tempFile.write(image);
var contents = tempFile.read();
Ti.API.log( 'contents: ' + contents.toString() ); // outputs "[object TiBlob]"
Ti.API.log( 'contents: ' + contents ); // outputs "[object TiBlob]"
Ti.API.log( 'contents: ' + JSON.stringify(contents) );
Ti.API.log( 'contents: ' + contents.text ) // outputs "null"
Is there any way to get the contents of a TiBlob as a string?
6 Answers
-
Apparently TiBlob is very sensitive to the contents…..if there is anything that is not ASCII, then it returns null for .text
-
I'm was using 1.7.2 and this was not working, so I downloaded the bleeding edge (1.7.5) and tried, still no luck. I opened up TiBlob.m and TiBlob.h to inspect, I added a function and got it to work
add this to TiBlob.m
-(NSString)textRep
{
NSData theData = [NSData dataWithContentsOfFile:path];//NSData* pictureData = UIImagePNGRepresentation(image); NSString* pictureDataString = [theData base64Encoding]; return pictureDataString;
}
and this to TiBlob.h
@property(nonatomic,readonly) NSString* textRep;now, clean and rebuild the project
you should now get a base64 encoded string when you access yourBlob.textRep
-
I was looking at this the past weekend, so I could use some existing APIs on my server that wanted an image as an array of bytes. Basically I came to the conclusion that you can't with the current Appcelerator Mobile SDK. You can on the Desktop apparently, but I ended up putting a new endpoint on my server and passing there Ti.Blob using XHR.
Maybe 1.5 will bring this feature? There is some cool stuff in the commit logs, including fixes for animations on Android. I drool I tell you! Drool!
(Though it did not really help me, you can save the stuff out as a temporary file and try to play with that. There is no file API to read data as chunks of bytes though, so making a byte array from that also turned out to be a dead end for me.)
-
tempFile.read().text
is not working for me, The File Im trying to read is of 30,708 bytes and containing text data
-
This worked for me
tempFile.read().text;
-
.text property worked for me