2012-04-06 63 views

回答

2

這可能是幫助完整。

public Bitmap getImage(){ 
    Bitmap bitmapImage=null; 
    try{ 
     InputStream input; 
     FileConnection fconn = (FileConnection) Connector.open("file:///store/home/user/dirname/imgname.png", Connector.READ_WRITE); 
     input = fconn.openInputStream(); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     int j = 0; 
     while((j=input.read()) != -1) { 
      baos.write(j); 
     } 
     byte[] byteArray = baos.toByteArray(); 

     bitmapImage = Bitmap.createBitmapFromBytes(byteArray,0,byteArray.length,1); 


    }catch(Exception ioe){ 
     System.out.println(ioe); 
    } 
    return bitmapImage; 
} 

享受..

+0

我想縮小位圖大小。怎麼可能? – 2012-04-06 14:24:27

+0

您好,請嘗試搜索..如果你解決問題,請標記爲true。http://stackoverflow.com/questions/1769755/blackberry-how-to-resize-image。 – 2012-04-06 19:12:45

0

試試這個示例代碼:如果您有任何疑問

public class LoadingScreen extends MainScreen implements FieldChangeListener 
{ 
private VerticalFieldManager ver; 
private ButtonField showImage; 
private BitmapField bitmapField;  

public LoadingScreen() 
{ 
    ver=new VerticalFieldManager(USE_ALL_WIDTH); 
    showImage=new ButtonField("Show Image",Field.FIELD_HCENTER); 
    showImage.setChangeListener(this);  
    ver.add(showImage); 

    bitmapField=new BitmapField(null,Field.FIELD_HCENTER); 
    bitmapField.setPadding(10, 0, 10, 0); 
    ver.add(bitmapField); 

    add(ver); 
} 
public void fieldChanged(Field field, int context) 
{ 
    if(field==showImage) 
    { 
     selectImageFromSDCARD(); 
    } 
} 

private void selectImageFromSDCARD() 
{ 
    String PATH=""; 
    if(SDCardTest.isSDCardAvailable())//sdcard available then    
     PATH = System.getProperty("fileconn.dir.memorycard.photos");//The default stored Images Path; 
    else      
     PATH = System.getProperty("fileconn.dir.photos");//The default stored Images Path; 

    FilePicker filePicker=FilePicker.getInstance(); 
    filePicker.setPath(PATH);  
    filePicker.setListener(new Listener() 
    {  
     public void selectionDone(String url) 
     { 
      System.out.println("======================URL: "+url); 
      try 
      {     
       FileConnection file = (FileConnection)Connector.open(url); 
       if(file.exists()) 
       { 
        InputStream inputStream = file.openInputStream(); 
        byte[] data=new byte[inputStream.available()]; 
        data=IOUtilities.streamToBytes(inputStream); 
        Bitmap bitmap=Bitmap.createBitmapFromBytes(data, 0, data.length,1);//Here we get the Image; 

        Bitmap scaleBitmap=new Bitmap(400, 300);//Now we are scaling that image; 
        bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS); 
        bitmapField.setBitmap(scaleBitmap); 
       } 
       else 
       { 
        bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png")); 
       } 
      } 
      catch (IOException e) 
      { 
       bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png")); 
      }    
     } 
    }); 
    filePicker.show();  
} 

protected boolean onSavePrompt() //It doesn't show the "Save","Discard","Cancel" POPUP; 
{ 
    return true; 
} 

public boolean onMenu(int instance) //It doesn't show the Menu; 
{ 
    return true; 
} 
} 

請參閱本博客:Get Image From SDcard

1

嗨,大家好上面的代碼是非常有用的對於BB OS> = 5.0 我正在使用可用於OS 4的代碼.2或更高。

private Bitmap resizeBitmap(Bitmap image, int width, int height) 
    { 

     int rgb[] = new int[image.getWidth()*image.getHeight()]; 
     image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); 
     int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height); 
     Bitmap temp2 = new Bitmap(width, height); 
     temp2.setARGB(rgb2, 0, width, 0, 0, width, height); 
     return temp2; 
    } 

    private int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) 
    { 
     int out[] = new int[x2*y2]; 
     for (int yy = 0; yy < y2; yy++) 
     { 
      int dy = yy * y/y2; 
      for (int xx = 0; xx < x2; xx++) 
      { 
       int dx = xx * x/x2; 
       out[(x2 * yy) + xx] = ini[(x * dy) + dx]; 
      } 
     } 
     return out; 
    } 
相關問題