How to use fileWriter.write() to write a binary file (a JPEG image)?

2,948 views
Skip to first unread message

Abraham Lopez

unread,
Oct 18, 2012, 8:12:29 PM10/18/12
to phon...@googlegroups.com
I'm trying to save images locally by:

1. Downloading the image using an XHR request.
2. Putting the image data into a BlobBuilder object, using the BlobBuilder.js library (https://github.com/eligrey/BlobBuilder.js) which emulates the BlobBuilder class in Safari Mobile.
3. Getting the blob object using imageBlob = BlobBuilder.getBlob("image/jpeg").
4. Saving the data using fileWriter.write(imageBlob);

This works PERFECTLY in Google Chrome, but fails silently on Safari Mobile. I tried putting the fileWriter.write() call within a try/catch block but it doesn't throw out any error messages, so I suspect the PhoneGap library just ignores the method call if an invalid argument is passed to write().

My theory is that this works perfectly in Chrome because it natively supports the BlobBuilder and Blob classes, whereas in Safari Mobile they have to emulated using the BlobBuilder.js library mentioned above.

What is the CORRECT way to save binary data to a file using PhoneGap?

A screenshot with an extract of my actual code is below:



Simon MacDonald

unread,
Oct 18, 2012, 9:24:55 PM10/18/12
to phon...@googlegroups.com
Sorry the FileWriter in PhoneGap can only write textual data.


--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
Ā 
For more info on PhoneGap or to download the code go to www.phonegap.com
Ā 
To compile in the cloud, check out build.phonegap.com
Ā 
Ā 

Abraham Lopez

unread,
Oct 18, 2012, 9:59:47 PM10/18/12
to phon...@googlegroups.com
Will it support binary data in the next version? The HTML5 File API implementation of Google Chrome works perfectly with writing binary data using Blob objects.

Also, what's the current actual problem with the PhoneGap API only supporting text? Does it automatically convert the data to UTF8 encoded text?

Kerri Shotts

unread,
Oct 19, 2012, 12:22:29 AM10/19/12
to phon...@googlegroups.com
Kinda hard to support when it's the act of sending data across the native bridge that causes the problem IIRC. This means binary data can't ever written directly to a file without using base64 and plugins. The closest you could get would be a plugin that used base64 -- you'd send it your base64-encoded binary data, and the plugin would decode it and write to the desired file.

Could be totally wrong on it, but that's how I understand the issue.

______________________________
Kerri Shotts
photoKandy Studios LLC

šŸ“± Phone:Ā +1 (312) 380-1035
🌐 Web: http://photokandy.com 

Twitter: @photokandy


On Oct 18, 2012, at 20:59, Abraham Lopez <ablop...@gmail.com> wrote:

Will it support binary data in the next version? The HTML5 File API implementation of Google Chrome works perfectly with writing binary data using Blob objects.

Also, what's the current actual problem with the PhoneGap API only supporting text? Does it automatically convert the data to UTF8 encoded text?

Abraham Lopez

unread,
Oct 19, 2012, 11:20:44 AM10/19/12
to phon...@googlegroups.com
That's a great idea.

On the other side, it would be really great if PhoneGap included a fileWriter.writeBinary() method that accepts a base-64 encoded string as input, decodes it, and writes it to the file as binary data.

Would it be possible to include this in the wishlist for a next version of PhoneGap? It will be really useful and absolutely necessary for handling commonly used non-text files like images, sound and video.

Steve Drucker

unread,
Jun 24, 2014, 7:26:14 AM6/24/14
to phon...@googlegroups.com

I've written a blog article that describes the procedure in Sencha Touch...you should be able to adapt it for native phonegap/cordova pretty easily:

Reply all
Reply to author
Forward
0 new messages