I need to load a generated image into my Java desktop application.
I am using similar code than this:
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
//it isn't the code here
}
The image it is loaded, but at bigger images my application just quit.
What to do to detect how big image can I load, or not just quit?
If you aren’t in an Event Dispatcher Thread ( like when a button in UI is pressed ) than your app will not crash. It will crash the Thread, which will be deallocated, you will not get the image but your app will be alive.
There is is possibility to create a Thread: or you will extend a Thread and override the run() method or create a Runnable interface and give it to a thread constructor.
Try to debug / log your code before enter in loading images function to see how much is your maximum allocable memory:
If you have 50 MB free and the file is 50MB, than no reason to try to load.