Search code examples
javajava-melwuit

How to set the value of a byte[] variable so that it can hold all an Image?


I have photos in the phone device. I want to get the byte[] value of a photo. I use this code to get it :

fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/", Connector.READ);
                            if (fcDir.exists())
                            {
                                Enumeration filelist = fcDir.list("*", false);
                                while (filelist.hasMoreElements())
                                {
                                    String fileName = (String) filelist.nextElement();
                                    fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+fileName, Connector.READ);
                                    dis = fcFile.openDataInputStream();
                                    byte[] rawImg = new byte[10240];
                                    dis.readFully(rawImg);
                                    Image tmpImg = Image.createImage(rawImg, 0, rawImg.length);
                                    new Fimage(tmpImg).show();
                                }
                            }

The Fimage LWUIT Form is shown successfully and it has the Image tmpImg as its background image : not all the Form is occupyied by the Image but only 3/4 of the Form.

My problem is that I do not know what number to set exactly for the argument at the line byte[] rawImg = new byte[10240]; . I set the argument to 10240 , but it is not very clever. So how to set exactly this number ?


Solution

  • The FileConnection class has a method fileSize() -- call it, and use the result as the size for the array.