2012-05-22 43 views
0

我們可以從SD卡圖像文件路徑創建一個Bitmap對象嗎?BlackBerry:從SD卡圖像創建位圖對象

我寫了一個示例方法來創建它。這是正確的方法嗎?

public Bitmap createBitmap(String filepath){  
    try { 
     // filepath e.g. "file:///SDCard/test.jpg" 
     FileConnection fc = (FileConnection)Connector.open(filepath); 
     byte[] responseData = new byte[10000]; 
     InputStream inputStream =fc.openDataInputStream(); 
     int length = 0; 
     StringBuffer rawResponse = new StringBuffer(); 
     while (-1 != (length = inputStream.read(responseData))) { 
      rawResponse.append(new String(responseData, 0, length)); 
     } 

     byte[] dataArray = rawResponse.toString().getBytes(); 
     Bitmap bitmap = Bitmap.createBitmapFromBytes(dataArray, 0, dataArray.length, 1); 
     // EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); 

     return bitmap; 
    } catch (IOException e) { 
     return null; 
     // TODO Auto-generated catch block 
     // e.printStackTrace(); 
    } 
} 

回答

0

公共最後一類自選擴展MainScreen {

Bitmap bit, bit1; 

BitmapField bitf[] = new BitmapField[12]; 

FileConnection fc; 

FlowFieldManager grid; 

String[] imgpath = { "bharat.png", "fighter1.jpg", "fighter2.jpg", 
     "fighter3.jpg", "fighter4.jpg", "fighter5.jpg", "fighter7.jpg", 
     "fighter8.jpg", "fighter9.jpg", "fighter10.jpg", "fighter11.jpg", 
     "fighter6.jpg" }; 
String compath = "file:///SDCard/";`enter code here` 

public MyScreen() { 
    grid = new FlowFieldManager(); 
    try { 

     setTitle("Bharat Title"); 

     for (int i = 0; i < imgpath.length; i++) { 

      fc = (FileConnection) Connector.open(compath + imgpath[i]); 
      DataInputStream in = fc.openDataInputStream(); 
      int size = (int) fc.fileSize(); 
      byte[] bb = new byte[size]; 
      bb = IOUtilities.streamToBytes(in); 
      bit = bit.createBitmapFromBytes(bb, 0, bb.length, 1); 
      bit1 = new Bitmap(120, 120); 
      bit.scaleInto(bit1, Bitmap.FILTER_BOX); 
      bitf[i] = new BitmapField(bit1, FOCUSABLE); 
      grid.add(bitf[i]); 
      fc.close(); 

     } 
    } 

    catch (Exception ex) 
    { 
     ex.getMessage(); 
    } 
    finally { 
     try { 
      fc.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    add(grid); 
} 

}

+0

我們可以創建與源圖像的高度和寬度的位圖? – user1407894