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
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.