03-11-2012 01:12 PM
Does anybody know a readFile and data manipulation tutorial?
Does utils.blobtoString still not work?
Solved! Go to Solution.
03-13-2012 06:04 PM
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/bla
If you expand a little on the goal, we might be able to give a more specific example.
Erik Oros
BlackBerry Development Advisor
03-13-2012 06:27 PM
Here this post discuse the same problem I do.http://supportforums.blackberry.com/t5/Web-and-Web
03-14-2012 02:29 PM - edited 03-14-2012 02:32 PM
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.
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 final output on my screen was as follows:
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
03-15-2012 03:38 AM
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?
03-15-2012 10:09 AM - edited 03-15-2012 10:10 AM
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.0 | OS 6.0 | OS 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
03-15-2012 01:17 PM
03-16-2012 02:40 PM
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
03-20-2012 06:46 PM
If you are working with binary files, see the solution I posted here:
03-23-2012 03:10 AM - edited 03-23-2012 03:14 AM
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.