展开全部
public class Test{
private ImageIcon getImageAsIcon(String imageFile){
try {
InputStream in = Test.class.getResourceAsStream(imageFile);
final byte[] imageByteBuffer = convertToByteArray(in);
in.close();
return new ImageIcon(imageByteBuffer);
} catch (Exception e) {
return null;
}
}
private static byte[] convertToByteArray(final InputStream is) {
int read = 0;
int totalRead = 0;
byte[] byteArray = new byte[MAX_IMAGE_SIZE];
try {
while ((read = is.read(byteArray,totalRead,MAX_IMAGE_SIZE - totalRead)) >= 0) {
totalRead += read;
}
} catch (Exception e) { return null; }
byte[] finalByteArray = new byte[totalRead];
System.arraycopy(byteArray,0,finalByteArray,0, totalRead);
return finalByteArray;
}
private ImageIcon getImageAsIcon(String imageFile){
try {
InputStream in = Test.class.getResourceAsStream(imageFile);
final byte[] imageByteBuffer = convertToByteArray(in);
in.close();
return new ImageIcon(imageByteBuffer);
} catch (Exception e) {
return null;
}
}
private static byte[] convertToByteArray(final InputStream is) {
int read = 0;
int totalRead = 0;
byte[] byteArray = new byte[MAX_IMAGE_SIZE];
try {
while ((read = is.read(byteArray,totalRead,MAX_IMAGE_SIZE - totalRead)) >= 0) {
totalRead += read;
}
} catch (Exception e) { return null; }
byte[] finalByteArray = new byte[totalRead];
System.arraycopy(byteArray,0,finalByteArray,0, totalRead);
return finalByteArray;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询