我使用javafx创建了一个图像查看器应用程序。我想将文件夹内的所有图像显示为缩略图。
使用以下代码时,发生java.lang.OutOfMemoryError:Java堆空间
错误。我也使用了System.gc()
,但没有任何改善。
GridPane thumbnails=new GridPane();
for (int row = 0; row < 1; row++) {
for (int column = 0; column <= jFiles.size(); column++) {
int newindex = column;
String currentBackImage = FileHandler.CURRENT_DIR + "/" + jFiles.get(column).fullname;
FileInputStream imageInput = new FileInputStream(new File(FileHandler.CURRENT_DIR + "/" + jFiles.get(column).fullname));
String ext = currentBackImage.toLowerCase().substring(currentBackImage.lastIndexOf('.') + 1);
if (ext.contains("tif")) {
FileChannel channel = imageInput.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
System.gc();
img = mainController.load(buffer.array());
imageInput.close();
} else {
System.gc();
img = new Image(imageInput);
imageInput.close();
}
StackPane stack = new StackPane();
ImageView view = new ImageView();
view.setImage(img);
stack.add(view);
thumbnails.add(stack, row, column);
imageInput.close();
}
}