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

Java Development

Reply
Highlighted
Regular Contributor
Posts: 77
Registered: ‎01-27-2010
My Device: Dev Alpha
My Carrier: Verizon
Accepted Solution

Problem with PersistentObject.

Hi all,

               I'm new in BB and poorly in english (I'm Thai), sorry if you confuse in my words. I have a project that has thead to download image from server and save it into PersistentObject by using my CustomHashTable. The problem is I recieve an error "NonPersistableObjectException" that founds on method "persistentObject.commit();" in "getEncodedImage()" when I debugging it, Here this is my code.

 

public class TestApp extends UiApplication{

public static void main(String[] args) {
UiApplication app = new TestApp();
app.enterEventDispatcher();
}

TestApp(){
pushScreen(new TestScreen());
}
}


public class TestScreen extends MainScreen{

EncodedImage encodedImage;
CustomHashTable persistentHashtable;
private DataInputStream dis;
private byte[] encodeData;
private ByteArrayOutputStream byteStream;
PersistentObject persistentObject = PersistentStore.getPersistentObject(0x61a0ab9f850b5c9bL);

TestScreen(){
super();
if (persistentObject.getContents() == null) {
persistentHashtable = new CustomHashTable();
persistentObject.setContents(persistentHashtable);
persistentObject.commit();
}else {
persistentHashtable = (CustomHashTable)persistentObject.getContents();
}
encodedImage = getEncodedImage("http://download.magikmobile.mobi/scodp/mpreview.do?idc=683979&size=140x90&","4952","hot");
encodedImage = scaleImage(encodedImage);

this.setTitle("Test my app");
this.add(new BitmapField(encodedImage.getBitmap()));
}

public EncodedImage scaleImage(EncodedImage image) {
int displayWidth = Fixed32.toFP(Display.getWidth());
int imageWidth = Fixed32.toFP(image.getWidth());
int scalingFactor = Fixed32.div(imageWidth, displayWidth);
EncodedImage scaledImage = image.scaleImage32(scalingFactor, scalingFactor);
return scaledImage;
}

public EncodedImage getEncodedImage(String url, String id, String catagory) {

EncodedImage enc = null;
if (persistentHashtable.containsKey("EncodeImage01")) {
enc = (EncodedImage)persistentHashtable.get("EncodeImage01");
}

if (enc != null) {
System.out.println(">>>>> image not null");
return enc;
} else {
System.out.println(">>>>> image null");
try {
String newURL = null;
while (url.indexOf("&") > 0) {
newURL = url.substring(0, url.indexOf("amp;"));
url = url.substring(url.indexOf("amp;") + 4);
url = newURL + url;
}

newURL = null;
String suffix = "";
HttpConnection httpConnection = (HttpConnection) Connector.open(url+suffix);
httpConnection.setRequestMethod(HttpConnection.GET);
if (httpConnection.getResponseCode() == 302) {
boolean b = true;
while (b) {
if (httpConnection.getResponseCode() == 200) {
b = false;
break;
}
httpConnection = (HttpConnection) Connector.open(httpConnection.getHeaderField("Location"));
}
}
dis = httpConnection.openDataInputStream();
int length = (int) httpConnection.getLength();
if (length > 0) {
encodeData = new byte[length];
dis.readFully(encodeData);
} else {
byteStream = new ByteArrayOutputStream();
int ch;
while ((ch = dis.read()) != -1) {
byteStream.write(ch);
}
encodeData = byteStream.toByteArray();
byteStream.close();
byteStream = null;
}
dis.close();
dis = null;
httpConnection.close();
httpConnection = null;
try {
enc = EncodedImage.createEncodedImage(encodeData, 0, encodeData.length);
} catch (OutOfMemoryError e) {
System.out.println(">>>> OutOfMemoryError");
e.printStackTrace();
return null;
} catch (NullPointerException e) {
System.out.println(">>>> NullPointerException");
e.printStackTrace();
return null;
} catch (IllegalArgumentException e) {
System.out.println(">>>>> IllegalArgumentException");
e.printStackTrace();
return null;
} catch (ArithmeticException e) {
System.out.println(">>>> ArithmeticException");
e.printStackTrace();
return null;
}
encodeData = null;
persistentHashtable.put("EncodeImage01", enc);
persistentObject.commit();
catagory = null;
id = null;
url = null;
return enc;
} catch (SecurityException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}

}


public class CustomHashTable extends Hashtable implements Persistable{

}

 

What should I do ?

 

Thanks you very much

~MazMellow~

 

 

Developer
Posts: 192
Registered: ‎09-23-2009
My Device: 9860/PlayBook/Dev Alpha
My Carrier: Plus GSM

Re: Problem with PersistentObject.

EncodedImage is not implementing Persistable interface.

 

Solution: Use getData() method in EncodedImage to get raw byte with image and put this in storage, to recreate image use createEncodedImage with byte array as a parameter.

 

 

Found my post helpfull? Like it! It solves your trouble? Help others and accept it as solution. Search before asking.
My apps: Secret Cod3s | Should my tablet be banned?
--
Jakub Nietrzeba
PGS Software S.A.
Your reliable partner in Poland - IT Outsourcing - BlackBerry, iPhone, Web and Standalone applications
Regular Contributor
Posts: 77
Registered: ‎01-27-2010
My Device: Dev Alpha
My Carrier: Verizon

Re: Problem with PersistentObject.

[ Edited ]

Hi Jakub,

 

Can I storage the image instead raw byte data?

 

My project must to download more images so I need to storage them into my CustomHashtable for convenient using. Is there anyway to do that?

 

 

Thx

~MazMellow~

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem with PersistentObject.

[ Edited ]

My timing meant this post was redundant - so I have removed it.

Developer
Posts: 192
Registered: ‎09-23-2009
My Device: 9860/PlayBook/Dev Alpha
My Carrier: Plus GSM

Re: Problem with PersistentObject.

I don't see any trouble there, hashtable will take any object as a second parameter for put function, so you may put hashtable into persisten storage (it is supported and commonly used):

 

Hashtable hash = new Hashtable();

byte[] data = image.getData();

hash.put("first", data);

....

...

...

....

byte[] data = (byte[])hash.get("first");

image = EncodedImage.createEncodedImage(data, ....);

 

 

 

Found my post helpfull? Like it! It solves your trouble? Help others and accept it as solution. Search before asking.
My apps: Secret Cod3s | Should my tablet be banned?
--
Jakub Nietrzeba
PGS Software S.A.
Your reliable partner in Poland - IT Outsourcing - BlackBerry, iPhone, Web and Standalone applications
Regular Contributor
Posts: 77
Registered: ‎01-27-2010
My Device: Dev Alpha
My Carrier: Verizon

Re: Problem with PersistentObject.

ummm.. maybe I should to storage the raw byte data.

 

It's okay !!   Thanks gluth and peter   Smiley Happy

 

~MazMellow~