当我尝试从手机内存中加载图像时,出现内存不足错误java/lang/OutOfMemoryError 或 nativedecodeImage。手机内存中的一些图像为 12kd,而另一些则约为 589kb 或 600kb。小尺寸的图像被提取到一个列表中,但是当涉及到大尺寸的图像时,它会抛出 OOM 错误..??
这是我的代码
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);
}
我得到错误的地方是
image = Image.createImage(imageData, 0, length);
有没有人对此有任何想法。我被这件事困住了几天。我正在使用 S40 设备诺基亚 311.Netbeans MIDP 2.0