Java me 内存不足错误将图像加载到库时本机图像解码错误

Java me 内存不足错误将图像加载到库时本机图像解码错误,java-me,Java Me,我正在尝试将图像从文件夹加载到我的应用程序中,我使用的代码是 FileConnection fc = null; DataInputStream in = null; DataOutputStream out = null; try { fc = (FileConnection)Connector.open("file:///e:/Images/Abc.jpg"); int length = (int)fc.fileSize();//possible loss of precision

我正在尝试将图像从文件夹加载到我的应用程序中,我使用的代码是

FileConnection fc = null;
DataInputStream in = null;
DataOutputStream out = null;

try {
  fc = (FileConnection)Connector.open("file:///e:/Images/Abc.jpg");
  int length = (int)fc.fileSize();//possible loss of precision may throw error
  byte[] data = null;
  if (length != -1) {
    data = new byte[length];
    in = new DataInputStream(fc.openInputStream());
    in.readFully(data);
  }
  else {
    int chunkSize = 112;
    int index = 0;
    int readLength = 0;
    in = new DataInputStream(fc.openInputStream());
    data = new byte[chunkSize];
    do {
      if (data.length < index + chunkSize) {
        byte[] newData = new byte[index + chunkSize];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
      }
      readLength = in.read(data, index, chunkSize);
      index += readLength;
    } while (readLength == chunkSize);
    length = index;
  }
  Image image = Image.createImage(data, 0, length);
  image = createThumbnail(image);
  ImageItem imageItem = new ImageItem(null, image, 0, null);
  mForm.append(imageItem);
  mForm.setTitle("Done.");
  fc = (FileConnection)Connector.open("file:///e:/Images/Abc.jpg");
  if(!fc.exists()){
      try{
      fc.create();                                                                                                                                                                              
      }catch(Exception ce){System.out.print("Create Error: " + ce);}
  }
  out = new DataOutputStream(fc.openOutputStream());
  out.write(data);
}
catch (IOException ioe) {
  StringItem stringItem = new StringItem(null, ioe.toString());
  mForm.append(stringItem);
  mForm.setTitle("Done.");
}
finally {
  try {
    if (in != null) in.close();
    if (fc != null) fc.close();
  }
  catch (IOException ioe) {}
FileConnection fc=null;
DataInputStream in=null;
DataOutputStream out=null;
试一试{
fc=(文件连接)连接器。打开(“file:///e:/Images/Abc.jpg");
int length=(int)fc.fileSize();//可能的精度损失可能引发错误
字节[]数据=null;
如果(长度!=-1){
数据=新字节[长度];
in=新的DataInputStream(fc.openInputStream());
in.readFully(数据);
}
否则{
int chunkSize=112;
int指数=0;
int readLength=0;
in=新的DataInputStream(fc.openInputStream());
数据=新字节[chunkSize];
做{
if(data.length
希望这可能是图像的问题,我试图调整图像的大小

    private Image createThumbnail(Image image)
{
    int sourceWidth = image.getWidth();
    int sourceHeight = image.getHeight();
    int thumbWidth = 18;
    int thumbHeight = 23;

    Image thumb = Image.createImage(thumbWidth, thumbHeight);
    Graphics g = thumb.getGraphics();

    for (int y = 0; y < thumbHeight; y++)
    {
    for (int x = 0; x < thumbWidth; x++)
    {
    g.setClip(x, y, 1, 1);
    int dx = x * sourceWidth / thumbWidth;
    int dy = y * sourceHeight / thumbHeight;
    g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);

    }
    }

    Image immutableThumb = Image.createImage(thumb);
    return immutableThumb;
} 
私有图像创建缩略图(图像)
{
int sourceWidth=image.getWidth();
int sourceHeight=image.getHeight();
int指宽=18;
拇指高度=23;
Image thumb=Image.createImage(拇指宽度、拇指高度);
Graphics g=thumb.getGraphics();
对于(int y=0;y

尽管如此,它还是返回了一个memmory native image deocde错误的异常,请有人帮我整理一下

这可能是由于两个原因造成的

  • 图像大小:在某些手机中,由于堆大小较小,设备无法加载图像,因此请尝试使用较小的图像大小

  • 第二个原因可能是图像已损坏,请尝试使用其他图像