1)从本地磁盘读取图片 2)将输入流转换为字节数组 3)使用字节数组创建Image对象

来源:互联网 发布:一列数据怎么求和 编辑:程序博客网 时间:2024/06/03 14:08

1)从本地磁盘读取图片

    FileConnection fc = (FileConnection) Connector.open("file:///map/zhuangshi07.png");//path是图片路径
                InputStream is = fc.openInputStream();
                byte bufferImage[] = new byte[(int) fc.fileSize()];
                bufferImage = InputStreamToByte(is);
                is.close();
                fc.close();

 

2)  将输入流转换为字节数组

     private byte[] InputStreamToByte(InputStream is) throws IOException { 
   ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); 
   int ch; 
   while ((ch = is.read()) != -1) { 
    bytestream.write(ch); 
   } 
   byte imgdata[] = bytestream.toByteArray(); 
   bytestream.close(); 
   return imgdata; 
  }

 

3)使用字节数组创建Image对象

    img = Image.createImage(bufferImage, 0, bufferImage.length);