10-13-2012 03:16 AM
I am developing a drawing app that includes a save file function. I want to save the image to an album in the general pictures directory on the playbook. Then the user should be able to view their saved drawing from a photo album in the picture directory. I have found all sorts of examples on how to do this for app shared directory (using blackberry.io.dir.appDirs.shared.camera.path) But, I haven't found out how to get to the general (non-app specific) pictures directory. How do I get to that directory using javascript?
Solved! Go to Solution.
10-13-2012 09:51 AM
My guess is that you want to use the following path for saving the pictures:
blackberry.io.dir.appDirs.shared.photos.path
If you ever want to find all shared directories you can do the following:
console.log(blackberry.io.dir.appDirs.shared);
On my PlayBook this gives:
Let me know if this works.
Cheers,
James
10-13-2012 04:59 PM
Thanks James. I tried using shared.photos instead of shared.camera, but this still doesn't solve my problem.
My ultimate goal is to get something stored into Pictures on the playbook. I want the user to be able to click on the Pictures icon, then be able to find his/her newly saved drawing in an album.
Here is a code snippet (using shared photos):
--------------------------------------------------
var currentImgData = currentCanvas.toDataURL("file/jpeg");
//convert currentImg to blob data
currentImgData = currentImgData.replace('data:image/jpeg;base64,', '');
//Testing save
//document.write('<img src="'+currentImgData+'"/>');
var currentBlobData = blackberry.utils.stringToBlob(currentImgData, 'binary');
alert("created blob data");
//Generate unique file name
var numRand = Math.floor(Math.random()*9999999);
alert ("random number generated: " + numRand.toString());
try{
var Dir = blackberry.io.dir;
var path = Dir.appDirs.shared.photos.path;
alert ("path = " + path);
}
catch(e)
{
alert("error in saveFile: " + e.name + " " + e.message)
}
var filename = path + "/JustDraw"+ numRand.toString() +".jpg";
alert ("Checking to see if file exists: " + filename);
while (blackberry.io.file.exists(filename)) {
//then generate new filename
numRand = Math.floor(Math.random()*9999999);
var filename = path + "/JustDraw/"+ numRand.toString() +".jpg";
}
//Save blob daga image to file
alert("Saving file to: " + filename);
try {
blackberry.io.file.saveFile(filename, currentBlobData);
//blackberry.io.file.saveFile(fileDir + "/test.jpeg", final_image_blob);
} catch (e) {
alert('e.message = ' + e.message);
}
alert ("file saved to: " + filename);
--------------------------------------------------
My blob data save seems to work, as I can write the image to the screen successfully. I get through all my alert messages with no errors. (I'm also having problems with web inspector hence all of the alerts.) According to my alert messages, my file is ultimately saved to:
file:///accounts/1000/appdata/jDrawApp...riduculou
This does not result in any new image being added to the playbook's pictures.
My problem is: how do I get the file saved to the Pictures directory, so that the user can directly access it outside of my app?
I am running OS 2.1.0.1032 on my blackberry. I have the following settings included in my config.xml file:
-----------------------------------
<rim
ermissions>
<rim
ermit>access_shared</rim
ermit>
</rim
ermissions>
<feature id="blackberry.io.file" required="true" />
<feature id="blackberry.io.dir" required="true" />
------------------------------------
-Jacquie
10-13-2012 05:02 PM - edited 10-13-2012 05:06 PM
well without the smiley faces which are actually '
' in my config file - nevermind
10-13-2012 06:38 PM
Hi Jacquie,
Sorry, I think I made a mistake. Can you try replacing this line of code:
var path = Dir.appDirs.shared.photos.path;
With the following:
var path = "file:///accounts/1000/shared/photos";
I looked over the rest of your code and it seemed fine.
10-13-2012 08:59 PM
Tried that -- doesn't work either. I also tried creating a new subdirectory in the shared/photos directory:
var path = "file:///accounts/1000/shared/photos/JustDraw";
if (!blackberry.io.dir.exists(path)){
blackberry.io.dir.createNewDir(path);
}But that doesn't work either.
10-16-2012 01:31 AM
Ok. I think this does work: var path = "file:///accounts/1000/shared/photos/JustDraw"
When I connect the playbook to my desktop and look at the playbook's file structure on my desktop, I see a folder named "JustDraw" in the Photos folder. However, the files that were saved to the JustDraw folder are damaged or corrupt. So, now I think my problem is in the blob or save parts.
-Jacquie
10-16-2012 01:21 PM
Hi Jacquie,
Glad the file is being saved. If your image is corrupted, did you edit the stringToBlob function so that it supports the binary data type? If not, look at the second post on this page by twindsor:http://supportforums.blackberry.com/t5/Web-and-Web
Cheers,
James
10-16-2012 04:35 PM
Almost! Using this method, I can save a .png file. But it doesn't work for saving a .jpg file
. I get undefined object when calling: blackberry.io.file.saveFile(filename, currentBlobData); This is a problem since my default background on a canvas is light grey, while the default background on a playbook photo is black. Guess I'll have to paint in the background before saving.
At any rate, my original question regarding the photos pathname has been answered. Thank you James!
-Jacquie