How to tackle OutOfMemoryError

158 views Asked by At

When I try to load images from phone memmory I get Out Of Memory errorjava/lang/OutOfMemoryError or nativedecodeImage . Some images in phone memory are of 12kd and while some others are around 589kb or 600kb. The small sized images are fetched to a list but when it comes to larger sized images it thrown the OOM error..??

This is my code

FileConnection finalConnection;
 try
    {     
        fc.close();
        finalConnection = (FileConnection) Connector.open(path, Connector.READ_WRITE);
        if(finalConnection.exists())
        {
            InputStream fis = finalConnection.openInputStream();
            long overallSize = finalConnection.fileSize();
            int length = 0;
            byte[] imageData = new byte[0];
            while (length < overallSize)
            {
                byte[] data = new byte[CHUNK_SIZE];
                int readAmount = fis.read(data, 0, CHUNK_SIZE);
                byte[] newImageData = new byte[imageData.length + CHUNK_SIZE];
                System.arraycopy(imageData, 0, newImageData, 0, length);
                System.arraycopy(data, 0, newImageData, length, readAmount);
                imageData = newImageData;
                length += readAmount;
            }
            fis.close();
            finalConnection.close();
            System.out.println("LENGTH IS " + length);
            if (length > 0)
            {                    
                image = Image.createImage(imageData, 0, length);

            }
        }
        else
        {
            System.out.println("NO PATH FOR IMAGE");
        }
    }
 catch (Exception e)
    {
        System.out.println("Image.createImage(imageData, 0, length) " +e.toString());
    }
 catch(Error e)
 {
     System.out.println("Image.createImage " + e);
 }        

The point where I get error is at

image = Image.createImage(imageData, 0, length);

Does anyone have any idea about this. Am stuck with this thing for few days. Am working on S40 devices Nokia 311. Netbeans MIDP 2.0

2

There are 2 answers

0
funkybro On

The clue is in the exception... the images you are opening are too large to hold in memory. Bear in mind the compressed size of the image does not necessarily indicate whether it can be held or not, it's actually to do with the number of pixels.

If it's too big there's not much you can do... if you want to display the image on screen you will need to do some processing on it first to shrink it, before you can load it in to memory.

0
mr_lou On

Most JavaME-only enabled devices are limited in that regard, and there's nothing you can do to load images in that size.

But maybe you don't need to.

Many cameras produce a thumbnail version of the pictures they take - and include this thumbnail inside the EXIF data. So when you need to display a list of photos on the phone, you don't load the whole photos. You just load the thumbnail data from the exif data in the file.

I only experimented briefly with this though, back in the day. Found a few example source codes online that was supposed to do the trick, but it rarely worked. This was probably partly because the phones I tested on didn't save a thumbnail in the exif data though. Maybe the Asha 311 does?