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

Adobe AIR Development

Reply
Developer
Posts: 71
Registered: ‎01-06-2011
My Device: Not Specified
Accepted Solution

Saving User Selected Backgrounds

In one of my apps, I let the user select a background using CameraRoll, but I can't figure out how to save the background so that it can be loaded as the background the next time that they open up the application.

 

Should I be using a SharedObject? (That's what I've been attempting.)

---
My Apps:
KeepUp (http://appworld.blackberry.com/webstore/content/26514?lang=en)
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving User Selected Backgrounds

hey tyrael,

 

yes the SharedObject class would work perfectly for this. Your best bet is saving the path to the image in the SharedObject and then simply setting the background of your app when it loads to the string thats saved in the SharedObject.

 

So when you allow the user to pick an image, you simply save the path to the SharedObject's entry for example:

 

 

var sharedObject:SharedObject = SharedObject.getLocal("MyApp");

(...)

sharedObject.data.appBackground = "/path/to/image.png";

sharedObject.flush();

 

 

and when you need to recall it do it the same way but in reverse:

 

 

var sharedObject:SharedObject = SharedObject.getLocal("MyApp");

(...)

var image:Image = new Image();

(...)

image.setImage(sharedObject.data.appBackground);

(...)

addChild(image);

 

hope that sheds some light. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 71
Registered: ‎01-06-2011
My Device: Not Specified

Re: Saving User Selected Backgrounds

Looks like I hit the "Accept as Solution" button a little too soon...

 I use the CameraRoll and a Loader to allow the user to pick a background from their own photos. When I set sharedObject.data.background to a path, how do I know what the path of the user selected image is?

---
My Apps:
KeepUp (http://appworld.blackberry.com/webstore/content/26514?lang=en)
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving User Selected Backgrounds

hey,

 

depending on how you are choosing the file and retreiving it from the camera roll -- it will decide how you end up using the SharedObject.

 

if you can post a sample code of how you are doing the camera roll and setting the background of the app initially i can help you with setting the background on load Smiley Happy good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 71
Registered: ‎01-06-2011
My Device: Not Specified

Re: Saving User Selected Backgrounds

[ Edited ]

Ok, when the app loads I have this:

var backimg:qnx.ui.display.Image = new qnx.ui.display.Image;
		
		var sharedback:SharedObject;
		sharedback = SharedObject.getLocal("appbackground");
		if (sharedback.data.background != null)
		{
			backimg.setImage(sharedback.data.background);
			addChild(backimg);
		}
		else
		{
			sharedback.data.background = "Assets/defaultbackground.jpg";
			sharedback.flush();
			backimg.setImage(sharedback.data.background);
			addChild(backimg);
		}

 

 and this:

var cameraroll:CameraRoll = new CameraRoll();
		cameraroll.addEventListener( MediaEvent.SELECT, imageSelected );

 

and when the user clicks a button, these events trigger:

		function choosephoto(event:MouseEvent):void
		{
			cameraroll.browseForImage();
		}
		
		function imageSelected( event:MediaEvent ):void
		{
			var imagePromise:MediaPromise = event.data;
			imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
			imageLoader.loadFilePromise( imagePromise );
		}
			
		function imageLoaded( event:Event ):void
		{
			imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoaded );  
			backimg.setImage(imageLoader.content);
		}

 

EDIT: imageLoader is a Loader

---
My Apps:
KeepUp (http://appworld.blackberry.com/webstore/content/26514?lang=en)
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving User Selected Backgrounds

hey,

 

try the following and see if this works:

 

 

 

		function choosephoto(event:MouseEvent):void
		{
			cameraroll.browseForImage();
		}
		
		function imageSelected( event:MediaEvent ):void
		{
			var imagePromise:MediaPromise = event.data;
			imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
			imageLoader.loadFilePromise( imagePromise );
		}
			
		function imageLoaded( event:Event ):void
		{
			imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoaded );  
			backimg.setImage(imageLoader.content);
			
			sharedBack.data.background = imageLoader.url;
			sharedBack.flush();
		}

 

that should set the background value for you -- the rest looks good. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 71
Registered: ‎01-06-2011
My Device: Not Specified

Re: Saving User Selected Backgrounds

Using imageLoader.url threw an error (access of undefined property), so I tried imageLoader.LoaderInfo.url and imageLoader.contentLoaderInfo.url. Neither of those caused errors, and the next time the app loads, sharedback.data.background != null, but the image does not load.

---
My Apps:
KeepUp (http://appworld.blackberry.com/webstore/content/26514?lang=en)
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving User Selected Backgrounds

ahh yah sorry -- misread your data types -- good work on the fix though.

 

as for the image not loading, can do do a trace(sharedback.data.background) and see what is outputted when your app fails to load the image?

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
New Developer
Posts: 17
Registered: ‎04-22-2011
My Device: Playbook
My Carrier: Fido

Re: Saving User Selected Backgrounds

You should physically copy the image to your own storage folder, as the real photo could be deleted any chance by user.

Developer
Posts: 71
Registered: ‎01-06-2011
My Device: Not Specified

Re: Saving User Selected Backgrounds

@JRab I would, but I haven't managed to connect my playbook to the debugger yet. It keeps asking for the correct host name or ip address. I put in my ip address and it says its wrong.

 

@desertfox How should I go about that?

---
My Apps:
KeepUp (http://appworld.blackberry.com/webstore/content/26514?lang=en)