Midlet class
NewClass1.javaCode:public void startApp() { Vector v = new Vector(); v.addElement("aaaa"); v.addElement("bbbb"); v.addElement("cccc"); NewClass1 nc = new NewClass1(); Vector c = nc.getImages(v); OpenRecordStore("imagestore"); for (int i = 0; i < c.size(); i++) storeImage((Image)c.elementAt(i)); CloseRecordStore(); } public void OpenRecordStore(String rmsNAme) { try { rs = RecordStore.openRecordStore(rmsNAme, true); } catch(Exception e){} } public void CloseRecordStore() { try { rs.closeRecordStore(); } catch(Exception e){} } public void storeImage(Image image) { int width,height; try { System.out.println(rs.getNextRecordID()); width = image.getWidth(); height = image.getHeight(); int imageData[] = new int[width*height]; image.getRGB(imageData, 0, width, 0, 0, width, height); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(outStream); for (int i = 0; i < imageData.length; i++) dataOut.writeInt(imageData[i]); rs.addRecord(outStream.toByteArray(), 0, outStream.toByteArray().length); dataOut.close(); outStream.close(); } catch(Exception e) {} }
the result of print is 1,2,2. only the first image gets stored. i checked the images. i created a form inside the startapp method and appended every image into it. i was able to see them no problem. i didnt even have to cast the elements to Image (formx.append(data.elementAt(0));Code:import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.io.ContentConnection; import javax.microedition.lcdui.Image; public class NewClass1 { Vector data1; public NewClass1 () { data1 = new Vector(); } public Vector getImages(Vector data) { for (int i = 0; i < data.size(); i++) { try { ContentConnection connection = (ContentConnection) Connector.open("http://localhost/tutorial/images/" + data.elementAt(i) + ".jpg"); DataInputStream iStrm = connection.openDataInputStream(); ByteArrayOutputStream bStrm = null; Image im = null; byte imageData[]; int length = (int) connection.getLength(); if (length != -1) { imageData = new byte[length]; iStrm.readFully(imageData); } else // Length not available... { bStrm = new ByteArrayOutputStream(); int ch; while ((ch = iStrm.read()) != -1) bStrm.write(ch); imageData = bStrm.toByteArray(); bStrm.close(); } im = Image.createImage(imageData, 0, imageData.length); data1.addElement(im); if (connection != null) connection.close(); if (iStrm != null) iStrm.close(); if (bStrm != null) bStrm.close(); } catch (Exception e) { e.printStackTrace(); } } return data1; } }
shouldnt it be 1 2 3? shouldnt i b able to store all the images? the sizes are small. 326 by 92 dimensions.
i checked the size of data1. it downloaded all 3 images.
if i try to print the details of the rms, i only get details for the first pic. the second one doesnt even get store. how come?
these two classes arent my final version of course. i no it looks a bit ugly but im just putting everything together for you.
Thank you.

Reply With Quote

