Results 1 to 2 of 2
  1. #1
    wildheart25c is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default cant store images

    Midlet class
    Java Code:
       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) {}
         }
    NewClass1.java
    Java 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;
        }
    }
    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));
    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? is the recordstore full alraedy? :S
    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.

  2. #2
    wildheart25c is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    i got it to work. friend suggested i download wtk 2.5.2. it worked. same code. i had no idea sdk 3 was buggy o.o

Similar Threads

  1. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  2. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  3. how to store images in access database
    By sridarshan in forum JDBC
    Replies: 11
    Last Post: 06-26-2010, 02:26 AM
  4. How do I store images in a zip file in Java
    By mglover in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2010, 03:53 PM
  5. Extract xml to store into db
    By palanikumark in forum Advanced Java
    Replies: 3
    Last Post: 06-06-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •