来自SD卡的Blackberry BitmapField

时间:2012-04-06 13:28:24

标签: blackberry bytearray sd-card bitmapfield

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

3 个答案:

答案 0 :(得分: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;
}

享受..

答案 1 :(得分:1)

Hi Guys上面的代码对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;
    }

答案 2 :(得分: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