I know that in order to get a single image with name already known I can just use
getClass().getResource()..
However, what if I have many images in a specific folder? I don't want to have to take every single Image name and call the getResource() method.
The following works on Desktop, but causes a crash on Android:
public void initializeImages() {
String platform = "android";
if(Platform.isIOS())
{
platform = "ios";
} else if(Platform.isDesktop())
{
platform = "main";
}
String path = "src/" + platform + "/resources/com/mobileapp/images/";
File file = new File(path);
File[] allFiles = file.listFiles();
for (int i = 0; i < allFiles.length; i++) {
Image img = null;
try {
img = ImageIO.read(allFiles[i]);
files.add(createImage(img));
} catch (IOException ex) {
Logger.getLogger(ImageGroupRetriever.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//Taken from a separate SO question. Not causing any issues
public static javafx.scene.image.Image createImage(java.awt.Image image) throws IOException {
if (!(image instanceof RenderedImage)) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
image = bufferedImage;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write((RenderedImage) image, "png", out);
out.flush();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new javafx.scene.image.Image(in);
}
Is there something else I need to consider in the directory structure?