-1

was initially using jfilechooser to upload jpg files into project but switched to jFileDialog because I wanted to get the images in thumbnail view. but when I run the project, I get this error msg:

javax.imageio.IIOException:Cant read input file!

this is the code below:

FileDialog fd = new FileDialog(this,"Choose a File",FileDialog.LOAD); 
    fd.setDirectory("C:\\");
    fd.setFile("*.jpg;*.jpeg");
    fd.setVisible(true);  
    filename = fd.getFile();
    if(filename==null){
    }else{
    try{
            File imgs =new File(filename);
            BufferedImage bufferedimage=ImageIO.read(imgs);
            BufferedImage thumbnail=Thumbnails.of(bufferedimage)
            .size(145, 141)
            .asBufferedImage();
            ByteArrayOutputStream os = new  ByteArrayOutputStream();
            ImageIO.write(thumbnail,"jpeg", os);
            InputStream is=new ByteArrayInputStream(os.toByteArray());
            ByteArrayOutputStream bos = new  ByteArrayOutputStream();
            byte[] buf =new byte[1024];
            try{
                for(int readNum; (readNum=is.read(buf))!=-1;){
                    bos.write(buf,0,readNum);
                    System.out.println("Read" +readNum+ "bytes,");
                }
            }catch(IOException ex){
                Logger.getLogger(null);
            }
            person_image=bos.toByteArray();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){
            }
        }

I think the problem is coming from this section:

File imgs =new File(filename);
BufferedImage bufferedimage=ImageIO.read(imgs);

it doesn't seem to get the file. please what am I doing wrong?

Rodney Nart
  • 121
  • 11

1 Answers1

0

I just tested it a bit, and the method FileDialog.getFile just returns the File name, not the complete path! To get the directory, you have to call FileDialog.getDirectory

The easiest way to fix this is to simply concatenate the directory and the file.

filename = fd.getDirectory() + File.seperator + fd.getFile();

I'm not completely sure you need File.seperator, because fd.getDirectory may already have the File seperator at the end, and you get multiple seperators.

To solve that, use:

filename = new File(new File(fd.getDirectory()), fd.getFile()).getPath();

Or just store the file directly without the .getPath at the end and do not store a String to the path.

Alexander Daum
  • 741
  • 4
  • 14
  • how do I run a JFileDialog in an internal frame? am getting an error in this line of code: "FileDialog fd = new FileDialog(this,"Choose a File",FileDialog.LOAD);" error message says: no suitable constructor found @Alexander – Rodney Nart Aug 24 '18 at 12:21
  • @RodneyNart since JInteralFrame does not extend Frame, it won't work, sorry – Alexander Daum Aug 24 '18 at 12:24
  • if I use jfilechooser, can you help me get a code to display the files in thumbnail view please @Alexander – Rodney Nart Aug 24 '18 at 12:33
  • @RodneyNart You might want to take a look at this post: https://stackoverflow.com/questions/4096433/making-jfilechooser-show-image-thumbnails, especially the accepted answer, as it is a bit faster than the original code, but it should work. – Alexander Daum Aug 24 '18 at 12:36
  • ok thanks. your efforts very well appreciated. please kindly reach me on rodneynart1@gmail.com. for business proposals @Alexander. – Rodney Nart Aug 24 '18 at 12:58