BlackBerry:从SD卡映像创建Bitmap对象

时间:2012-05-22 10:06:35

标签: blackberry

我们可以从SD卡图像文件路径创建Bitmap对象吗?

我已经编写了一个示例方法来创建它。这是正确的方法吗?

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

1 个答案:

答案 0 :(得分:0)

公共最终类MyScreen扩展了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);
}

}