返回顶部

收藏

读取jar包中的图片的代码

更多

如下代码,使用getResourceAsStream返回jar中文件的InputStream流:

Image img = null;

try {
  MediaTracker m = new MediaTracker(this);
  InputStream is = getClass().getResourceAsStream("image.gif");
  //
  // if your image is in a subdir in the jar then
  //    InputStream is = getClass().getResourceAsStream("img/image.gif");
  //  for example
  //
  BufferedInputStream bis = new BufferedInputStream(is);
  // a buffer large enough for our image
  //
  // can be
  //   byte[] byBuf = = new byte[is.available()];
  //   is.read(byBuf);  or something like that...
  byte[] byBuf = = new byte[10000]; 

  int byteRead = bis.read(byBuf,0,10000);
  img = Toolkit.getDefaultToolkit().createImage(byBuf);
  m.addImage(img, 0);
  m.waitForAll();
  }
 }
catch(Exception e) {
  e.printStackTrace();
}

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论