2013-08-20 16 views
0

我想從一個文件夾加載圖像到我的應用程序,我使用的代碼是內存不足錯誤的固有圖像解碼錯誤的,同時加載圖像畫廊

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) {} 

希望它可以與圖像問題我試圖調整圖像大小

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; 
} 

不過它返回一個異常出memmory本地圖像deocde錯誤的,有人請幫我整理出來

回答

0

這可能監守兩個原因發生。

  1. 圖像尺寸:在一些電話由於小堆大小不設備不加載圖像,從而具有更小的圖像尺寸嘗試。

  2. 第二個原因可能是圖像已損壞,請嘗試使用其他圖像。

相關問題