【问题标题】:Blackberry BitmapField from SD cardSD卡中的Blackberry BitmapField
【发布时间】:2025-11-29 01:45:01
【问题描述】:

我想在 BitmapField 中显示 SD 卡中的图像。怎么做?谁能给我一些示例代码?

【问题讨论】:

    标签: blackberry bytearray sd-card bitmapfield


    【解决方案1】:

    这可能是帮助已满。

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

    享受..

    【讨论】:

    • 我想缩小位图大小。怎么可能?
    • 您好,请尝试搜索。*.com/questions/1769755/…如果您解决了问题,请标记为true。
    【解决方案2】:

    大家好,上面的代码对 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;
        }
    

    【讨论】:

      【解决方案3】:

      试试这个示例代码:

      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

      【讨论】: