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
Contributor
genration
Posts: 34
Registered: ‎05-28-2011
My Device: Blackberry Playbook
My Carrier: O2

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Right, a little more info...

I have spent a bit more time with this and trawled the source for both SDKs.

First thing is first when specifying BASE64 as the encoding to stringToBlob is does not decode the base 64 string you provide, it encodes it. For blobToString it is the opposite (decodes it). This seems rather illogical to me and it is not exactly what the documentation implies it does. You can try and pass another char encoding and decode the data yourself first using atob but then the data is butchered using adobe air FileStream class writeMultiByte function. There is only one solution which I have come across (which is cross platform) and it isn't graceful at all. If you are just going to develop for the pb then you could write your own webworks extension. There is a tutorial for this.

In terms of the cross platform way...

Replace the mime type e.g. image/png in the uri with image/octet-stream and the set the document.location.href to the data uri string. This seems to come with other issues though which I haven't (had time) fully understood yet.
Please use plain text.
Developer
laurentC
Posts: 266
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Hi, I'm trying to do the same but using dirs.shared.photos.path is not working. Dirs.shared.cameras.path is OK. The savefile function works but I can't find the image that should be saved???

 

Like the scrapbook app on PlayBook I would like to create my images in a directory but I'm not sure how I can do that? 

 

Please help.

 

Laurent

 

Twitter : @LaurentKP
My apps
Please use plain text.
Developer
laurentC
Posts: 266
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Ok I can save my images now.

I can't see them with the native Photos viewer app because they are not valid image files.

But if I use a file browser like Air Browser, I can find them.

 

So RIM guys what's wrong with toDataUrl or stringToBlob ? Please help us.

 

Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

It could be an issue with how the image is being retrieved/saved. Could you post a code snippet that reproduces the issue and also let us know which device and OS this was tested with?

 

Thanks,

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Developer
laurentC
Posts: 266
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

function takePicture() {
try {
blackberry.media.camera.takePicture(successCB, closedCB, errorCB);
} catch(e) {
alert("Error in supported: " + e);
}
}

 

function successCB(filePath) {
try {

 

var myCanvas = document.createElement("canvas");
myCanvas.width = 300;
myCanvas.height = 300;
var mycanvas_context = myCanvas.getContext("2d");

 

var image01 = new Image();
image01.onload = function(){
mycanvas_context.drawImage(image01,0,0);
};
image01.src=filePath;

 

final_image = myCanvas.toDataURL("image/png");

final_image_blob = blackberry.utils.stringToBlob(final_image,"BASE64");

blackberry.io.file.saveFile(image_path, final_image_blob);

}
catch(e) {
debug.log("onPhotoCaptured", e, debug.exception);
}

}

 

I'm using webworks 2.2.0.5 and Tablet OS 2.0.0.7971

Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

This line:

blackberry.media.camera.takePicture(successCB, closedCB, errorCB);

 

Results in the picture being taken and saved to the device /shared directory where it will be automatically read by the Pictures app. From your code it looks like you are trying to re-save them in the successCB section, which is not necessary.

 

Try removing the file access code to verify that the file gets saved successfully.

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Developer
laurentC
Posts: 266
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Yes it's not necessary to resave it but it should work right ? It should be saved with a different size too (300 x 300).
Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Sorry, your sample was a bit overcomplicated for the issue I *think* you're trying to show. Are you just trying to save an image file from the page source to the file system?

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Developer
laurentC
Posts: 266
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

I'm trying to convert a canvas to an image and save it to the file system.
Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry.io.file.saveFile using canvas toDataUrl and blackberry.utils.stringToBlob

Have you checked the file from the PlayBook to verify the format is correct? Do you have something similar working for the Desktop Browser? Seems like it could be an encoding issue.

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.