0

我们可以Bitmap从 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();
    }
}
4

1 回答 1

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

}

于 2012-05-22T11:31:22.883 回答