Open Image with My Application

Asked

Viewed 851 times

0

I am developing a java application to open image files with the class File(), would like to make my application the standard for opening the images on my computer, do not know how to receive by parameter the file path to my application.

You could help me, Grateful.

  • could better detail the scenario and/or add parts of the code?

1 answer

1


First, let’s start with a viewer code:

import java.awt.image.BufferedImage;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Visualizador {

    public static void main(String[] args) {
        if (args.length == 0) {
            JOptionPane.showMessageDialog(null, "Escolha um arquivo para abrir.", "Visualizador", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (args.length != 1) {
            JOptionPane.showMessageDialog(null, "Não foi possível entender a linha de comando.", "Visualizador", JOptionPane.ERROR);
            return;
        }
        BufferedImage img;
        try {
            img = lerImagem(args[0]);
            if (img == null) throw new IOException();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar o arquivo " + args[0] + ".", "Visualizador", JOptionPane.ERROR);
            return;
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível ler o arquivo " + args[0] + ".", "Visualizador", JOptionPane.ERROR);
            return;
        }
        EventQueue.invokeLater(() -> {
            JLabel jl = new JLabel(new ImageIcon(img));
            JFrame jf = new JFrame(args[0]);
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jf.add(jl);
            jf.pack();
            jf.setLocationRelativeTo(null);
            jf.setVisible(true);
        });
    }

    private static BufferedImage lerImagem(String s) throws FileNotFoundException, IOException {
        return lerImagem(new File(s));
    }

    private static BufferedImage lerImagem(File f) throws FileNotFoundException, IOException {
        if (!f.exists()) throw new FileNotFoundException();
        return ImageIO.read(f);
    }
}

Note that the file name is read from the command line.

Compile with this:

javac -encoding UTF8 Visualizador.java

Perform like this:

java Visualizador teste.png

Where teste.png is your image. If the image exists and is well formed, it will load. If it is not, a cute and friendly error message appears.

The programs that windows associates to file types are .exe, .com, .bat, .pif and .cmd. How does that not include .jar or .class, create that file visualizador.bat:

java Visualizador %1

So you can run it like this:

visualizador teste.png

Now, let’s associate the file. First, access the file type you want and go to the properties menu:

PNG

And then, click the button to change the default program:

Tela de propriedades

And look up your application (in the above case the visualizador.bat) in the list or click the button to find it somewhere else on your PC:

Lista

Arquivo

With this, by double clicking on the image, your program opens.

  • 1

    Thank you @Victor, your reply was exactly what you were looking for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.