Copy and write image launches java.lang.IllegalArgumentException: image == null!

4

I've created an algorithm to go through a directory tree and mirror its contents. The exception occurs when the program enters a directory with 400 images. It makes the copies of the images until it reaches an image around 320 and throws the following exception, in the method copyFile, conditional isImageJPG:

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
at javax.imageio.ImageIO.getWriter(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at Main.copiaArquivo(Main.java:53)
at Main.entraDiretorio(Main.java:44)
at Main.entraDiretorio(Main.java:42)
at Main.entraDiretorio(Main.java:42)
at Main.entraDiretorio(Main.java:42)
at Main.main(Main.java:31)

The code is as follows:

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.imageio.ImageIO;

public class Main {

private static File raizDiretorio;
private static File raizDiretorioCP;
private static String caminho = "D:\Ultimate Collection\info\xyz\";

public static void main(String[] args) throws IOException {

    raizDiretorio = new File(caminho);
    raizDiretorioCP = new File(caminho + "copia\");
    raizDiretorioCP.mkdir();

    File[] files = raizDiretorio.listFiles();

    entraDiretorio(files);

}

private static void entraDiretorio(File[] files) throws FileNotFoundException, IOException {
    for (File file : files) {
        if (file.isDirectory() && !file.getAbsolutePath().equals(raizDiretorioCP.getAbsolutePath())) {
            File[] listSubDiretorio = file.listFiles();
            file = new File(preparaCaminhoFileSaida(file));
            System.out.println("Criando diretorio " + file.getAbsolutePath());
            file.mkdir();
            entraDiretorio(listSubDiretorio);
        } else if (file.isFile() && !file.getAbsolutePath().equals(raizDiretorioCP.getAbsolutePath())) {
            copiaArquivo(file);
        }
    }
}

private static void copiaArquivo(File file) throws FileNotFoundException, IOException {
    String caminhoSaida = preparaCaminhoFileSaida(file);
    if (isImagemJPG(file)) {
        BufferedImage img = ImageIO.read(file);
        System.out.println("Copiando imagem de :" + file.getPath() + "\n para: " + caminhoSaida);
        ImageIO.write(img, "jpg", new File(caminhoSaida));

    } else

    {

        File outFile = new File(caminhoSaida);
        if (!outFile.exists() && outFile.isDirectory())
            outFile.mkdir();
        System.out.println("Copiando arquivo: " + file.getAbsolutePath());
        OutputStream ou = new FileOutputStream(outFile);
        OutputStreamWriter osw = new OutputStreamWriter(ou);
        BufferedWriter bw = new BufferedWriter(osw);

        String texto = "";
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String linha = reader.readLine();
        while (linha != null) {
            texto += linha;
            linha = reader.readLine();
        }

        texto = texto.replaceAll("nomealeatorio", "");

        bw.write(texto);
        bw.close();

    }

}

private static boolean isImagemJPG(File file) {
    return file.getName().endsWith(".jpg");
}

private static String preparaCaminhoFileSaida(File file) throws IOException {
    String path = file.getAbsolutePath();
    path = path.replace(caminho, raizDiretorioCP.getCanonicalPath() + "\");
    return path;
}
}

If I put a conditional to skip this photo that throws the exception the program continues but after some photos it re-launches the exception in another photo. I already checked the extension of the photos and they are all jpg. There is no other type of file in the directory. I'm not understanding why the algorithm works to some extent and then not.

    
asked by anonymous 21.08.2016 / 20:51

1 answer

5

From the documentation for ImageIO.read :

  

Returns a BufferedImage as a result of the decoding of the File supplied with an automatically chosen ImageReader among those currently registered. If no% of registered% claims to be able to read the resulting stream, ImageReader is returned [my emphasis].

null is null for some of your img files (you can confirm this by printing jpg ). This is probably because the img method is not able to read some of the files. I can not tell you exactly what is making ImageIO choke, but I know this class does not behave well with CMYK and some ICC color profiles .

That said, I did not understand why you're trying to "interpret" the files if the purpose is just to copy them from one place to another. If you do not want to change the files a direct copy method like the one below should solve your problem:

Files.copy(file.toPath(), new File(caminhoSaida).toPath());
    
21.08.2016 / 21:31