Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
arqlz
Posts: 26
Registered: ‎03-06-2012
My Carrier: 8298611647
Accepted Solution

ReadFile and data manipulation

Does anybody know a readFile and data manipulation tutorial?

Does utils.blobtoString still not work?

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,453
Registered: ‎04-12-2010
My Carrier: Bell

Re: ReadFile and data manipulation

Hi there,

 

What sort of data manipulation are you looking to do? There are a few snippets for readFile available here:

https://bdsc.webapps.blackberry.com/html5/apis/blackberry.io.file.html

 

If you expand a little on the goal, we might be able to give a more specific example.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
New Developer
arqlz
Posts: 26
Registered: ‎03-06-2012
My Carrier: 8298611647

Re: ReadFile and data manipulation

  1. Read a file. Example.txt
  2. Modify the string
  3. Save it again.

Here this post discuse the same problem I do.http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/WebWorks-SDK-v2-1-0-and-File-API...


 

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,453
Registered: ‎04-12-2010
My Carrier: Bell

Re: ReadFile and data manipulation

[ Edited ]

Hello arqlz,

 

The issue described in that article was due to an unimplemented blobToString method at the time. This has since been seemingly fixed. I just tested the following sample with success on my PlayBook.

 

Note that before this sample could run, I had to manually create the expected readme.txt file on the PlayBook's filesystem under:

\media\downloads\

 

To complete the above, I went into my PlayBook's Storage and Sharing settings and enabled File Sharing, as well as Wi-Fi Sharing (so I could create the file over-the-air from my PC.) I then created the file manually and populated it with the text:

Hello World!

 

My config.xml looks as follows.

 

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0">
	<name>Sandbox</name>
	<author>Oros</author>
	<content src="index.html"/>
	<access uri="*" subdomains="true"/>

	<feature id="blackberry.io.file" />
	<feature id="blackberry.utils" />
	
	<rim:permissions>
		<rim:permit>access_shared</rim:permit>
	</rim:permissions>
</widget>

 

The majority of this is just a standard configuration.

 

  • The <access> whitelisting isn't required but I included it out of habit.
  • The blackberry.io.file <feature> gives me access to readFile and saveFile.
  • The blackberry.utils <feature> gives me access to the blobToString and stringToBlob methods.
  • The access_shared permission ensures we can access the shared folder on the PlayBook.

My index.html file looks like so.

 

<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	</head>
	<body>
		<div id="log"></div>
		
		<script type="text/javascript">
			/*global document, blackberry */

			function log(text) {
				"use strict";
				var div = document.createElement("div");
				div.innerHTML = text;
				document.querySelector("#log").appendChild(div);
			}

			function handleOpenedFile(fullPath, blobData) {
				"use strict";
				try {
					log("File opened.");
					var filepath = 'file:///accounts/1000/shared/downloads/test.txt',
						stringData = blackberry.utils.blobToString(blobData);
					log("Write path: " + filepath);
					log("Read data: " + stringData);
					
					stringData = "Modified: " + stringData;
					log("Write data: " + stringData);
					blobData = blackberry.utils.stringToBlob(stringData);
					log("Write blob created.");
					blackberry.io.file.saveFile(filepath,  blobData);
					log("File saved.");
				} catch (err) {
					log("handleOpenedFile: " + err);
				}
			}

			function ready() {
				"use strict";
				try {
					log("Ready.");
					var path = 'file:///accounts/1000/shared/downloads/readme.txt';
					log("Read path: " + path);
					blackberry.io.file.readFile(path, handleOpenedFile);
					log("Read done.");
				} catch (err) {
					log("ready: " + err);
				}
			}

			document.addEventListener("DOMContentLoaded", ready, false);
		</script>
	</body>
</html>

 

From top to bottom, a brief description of the major pieces:

 

  • The #log <div> will hold our logging messages so we can keep track of what is happening.
  • The /*global */ definitions are there just to satisfy JSLint. Basically it announces that those variables are defined elsewhere.
  • The log function takes text and adds it within its own <div> to #log. You'lll notice a lot of calls to this function dispersed through the code; basically I just like to keep track of what's happening. These calls are not necessary though.
  • The handleOpenedFile function gets invoked if we successfully execute blackberry.io.file.readFile. This is where we do our manipulation of the read data. The manipulation converts the blob to a String, prepends the text "Modified: " to the String, then converts that String back to a blob which is then saved to our output file: test.txt.
  • The ready function gets invoked when the DOMContentLoaded event is triggered. We supply our default readme.txt filepath then attempt to read the data.
  • Finally, outside of the functions, we register the DOMContentLoaded event to trigger our ready function. We place it outside of all functions to ensure the event is registered when the <script> is loaded.

The final output on my screen was as follows:

 

IMG_00000002.PNG

 

And I can see on my PlayBook's filesystem that test.txt was created and contains:

Modified: Hello World!

 

If you have any questions about the above, please let me know.

 

Erik Oros

BlackBerry Development Advisor

 


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
New Developer
arqlz
Posts: 26
Registered: ‎03-06-2012
My Carrier: 8298611647

Re: ReadFile and data manipulation

Thanks!

 

I was confused, i've done all that before; It appears it only work with txt files, because i tried with jpg's, zip, doc, epub, mp3 and non of em return anything usable from the blobToString function.

 

What im missing?

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,453
Registered: ‎04-12-2010
My Carrier: Bell

Re: ReadFile and data manipulation

[ Edited ]

Hello arqlz,

 

I believe this will have to do with the encoding parameter in the blogToString method:

 

static String blobToString(blob : Blob, [encoding: String])

 

It may also have to do with blob size limits depending on the file you are loading:

 

 OS 5.0OS 6.0OS 7.0
BASE64 <= 89,678 bytes
Other Encodings <= 524,288 bytes (512KB) <= 2,097,152 bytes (2MB) <= 524,236 bytes (511KB)

 

I will try and modify my sample to work with one of the file types you noted.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
New Developer
arqlz
Posts: 26
Registered: ‎03-06-2012
My Carrier: 8298611647

Re: ReadFile and data manipulation

Any news?
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,453
Registered: ‎04-12-2010
My Carrier: Bell

Re: ReadFile and data manipulation

Hello arqlz,

 

I've given this a shot with all of the supported encodings and a PNG file. Essentially just attempting a straight copy (i.e. blobToString then stringToBlob.)

 

Unfortunately regardless of the encoding, the String that is created does not capture all of the file contents, thus recreating a Blob from the String does not generate any useful item.

 

What I think would be required here is to read the data in as bytes and do processing on that (as opposed to just reading the text) which I do not believe exists within the HTML5/WebWorks space at this time.

 

If anybody in the community has any thoughts here, feel free to jump in.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
BlackBerry Development Advisor
twindsor
Posts: 739
Registered: ‎07-15-2008
My Carrier: Bell

Re: ReadFile and data manipulation

If you are working with binary files, see the solution I posted here:

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/stringToblog-save-image-problem/...

Tim Windsor
Application Development Consultant
Please use plain text.
New Developer
arqlz
Posts: 26
Registered: ‎03-06-2012
My Carrier: 8298611647

Re: ReadFile and data manipulation

[ Edited ]

What's i'm trying to do is read data, so the solution you gave me doesn't work, i decided to use the custom.file extension tutorial, changed the directory to File.userDyrectory and made a binary to base64 converter ( I think the playbook use an iso-88xx-1 encoding so the string returned is unusable as I got it, I had to transcode it first). It read the data.  I have not tested resave the data back.

Please use plain text.