Reputation: 141
I am using a FileChooser for selectiong image from the system.Now I want to store the image into db.For this I have created a new folder image in the project,and my idea is load the folder with the opened images renamed like img1,img2...and store this path into the db, at a time one image. I used the code below for getting the image name
String name = jFileChooser1.getSelectedFile().getAbsolutePath();
But I dont know how to store the image into the folder.Hope anyone will help to solve this.
Upvotes: 2
Views: 6639
Reputation: 9
The following code selects image and saves it into a folder. For more follow this link How to save image in folder and path to database
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int res=chooser.showOpenDialog(ImageTutorial.this);
if(res==JFileChooser.APPROVE_OPTION)
{
file=chooser.getSelectedFile();
String path=file.getAbsolutePath();
ImageIcon image=new ImageIcon(file.getAbsolutePath());
Rectangle rec=imagelbl.getBounds();
Image scaledimage=image.getImage().getScaledInstance(rec.width,rec.height,Image.SCALE_SMOOTH);
image=new ImageIcon(scaledimage);
imagelbl.setIcon(image);
imagename.setText(file.getName());
}else
{
JOptionPane.showMessageDialog(null,"No new image selected");
}
Upvotes: 0
Reputation: 208944
I don't see the point of storing the images into your project jar. It would make more sense to save it into the system. But then again, why even do that?
Just store the image directly to the database. To do that just do this
PreparedStatement ps=con.prepareStatement("insert into imgtable values(?)");
FileInputStream fin=new FileInputStream(fileFromChooser);
ps.setBinaryStream(1,fin,fin.available());
int i=ps.executeUpdate();
System.out.println(i+" records affected");
UPDATE
Saving to File system. You need to specify the path in this line of code
ImageIO.write(image, "jpg",new File("C:\\path\\to\\destination\\" + file.getName()));
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
public class SaveImageFile {
public SaveImageFile() {
final JFrame frame = new JFrame("Save Image");
JButton saveImage = new JButton("Browse");
saveImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG, GIF, and PNG Images", "jpg", "gif", "png");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println("You chose to open this file: "
+ file.getName());
BufferedImage image;
try {
image = ImageIO.read(file);
ImageIO.write(image, "jpg",new File("C:\\path\\to\\destination\\" + file.getName()));
} catch (IOException ex) {
Logger.getLogger(SaveImageFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
frame.add(saveImage);
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SaveImageFile saveImageFile = new SaveImageFile();
}
});
}
}
Upvotes: 2