I draw an image on a panel by using BufferedImage, and now I want to export that image.
But how can I detect if the new file is created or replace the old one? right now my output is:
old filename: image.jpeg
new filename: image.jpeg.jpeg
How can I do it?? I put a detect code after the file is created, using createNewFile method, but it doesn't seem to work :(
This is pattern that do the saving, user can choose various types of image (bmp, jpeg ...):
imageFile is File
private void saveImage(){
JFileChooser savefile = new JFileChooser("~/");
savefile.setFileSelectionMode(JFileChooser.FILES_ONLY);//Chose file only
savefile.setFileFilter(new pngSaveFilter());//Save in PNG format
savefile.addChoosableFileFilter(new jpegSaveFilter());//Save in JPEG format
savefile.addChoosableFileFilter(new bmpSaveFilter());//Save in BMP format
savefile.addChoosableFileFilter(new gifSaveFilter());//Save in GIF format
savefile.setAcceptAllFileFilterUsed(false);
int returnVal = savefile.showSaveDialog(null);//Show save dialog
String EXT="";
String extension="";
if (returnVal == JFileChooser.APPROVE_OPTION) {
imageFile = savefile.getSelectedFile();
extension = savefile.getFileFilter().getDescription();
if (extension.equals("JPEG file images *.jpeg,*.JPEG")) {
EXT = "JPEG";
imageFile = new File(imageFile.getAbsolutePath() + ".jpeg");
}
if (extension.equals("PNG file images *.png,*.PNG")) {
EXT = "PNG";
imageFile = new File(imageFile.getAbsolutePath() + ".png");
}
if (extension.equals("Bitmap file images *.bmp,*.BMP")) {
EXT = "BMP";
imageFile = new File(imageFile.getAbsolutePath() + ".bmp");
}
if (extension.equals("GIF file images *.gif,*.GIF")) {
EXT = "GIF";
imageFile = new File(imageFile.getAbsolutePath() + ".gif");
}
try {
if(imageFile != null){
topViewImagePanel.drawToSave();
System.out.println(imageFile.createNewFile());
//ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
// the code detection is below
if (imageFile.createNewFile()){
int value = JOptionPane.showConfirmDialog(null, "Image existed! Replace?", "Warning!", JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION){
imageFile.delete();
ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
}else if (value == JOptionPane.NO_OPTION){
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(imageFile.exists())
Do a simple check to see if a file already exists or not.
Don't append file extension manually to the file name.
It is already present in the absolute path.
To handle files already present, use else clause of
if (imageFile.createNewFile())
Hope this helps.
If you use Java 7, use the new API:
if (imageFile != null) {
final Path path = imageFile.toPath();
if (Files.exists(path)) {
int value = JOptionPane.showConfirmDialog(null,
"Image existed! Replace?", "Warning!", JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION)
Files.delete(path);
}
ImageIO.write(topViewImagePanel.getSavingImage(), EXT,
Files.newOutputStream(path));
}
Also, as to your original question:
I put a detect code after the file is created, using createNewFile method, but it doesn't seem to work :(
This is normal, you call .createNewFile() twice:
System.out.println(imageFile.createNewFile()); // <-- CALL 1
//ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
// the code detection is below
if (imageFile.createNewFile()){ // <-- CALL 2
It will always fail the second time!
Related
Here is My Code the way I'm creating my folder in app Specific directory.
private File createDirectory(String dirName) {
File file = new File(getApplicationContext().getExternalFilesDir(dirName) + "/" + dirName);
if (!file.exists()) {
file.mkdir();
}
return file;
}
your method returns File, which is a directory. now you have to create new image file object
File dir = createDirectory("imagesDirectory");
File file = new File(dir.getAbsolutePath(), "imageName.jpg");
FileOutputStream outStream = new FileOutputStream(file);
now you have to compress your Bitmap as JPG (as name set above) and feed this stream
Bitmap pictureBitmap = ...; // your image
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
and don't foregt to close stream
outStream.flush();
outStream.close();
if you want lossless format you can use PNG (Bitmap.CompressFormat.PNG), don't forget to name your file extension accordingly
you can only write in your app-specific files
File dir_ = new File(context.getFilesDir(), "YOUR_DIR");
dir_.mkdirs();
you can use the function "getExternalFilesDir" and you change the value between in parenthesis. Here I put "Environment.DIRECTORY8DCIM" but there are others (you can check in the android developer web site).
I show you an exemple:
private fun getOutputMediaFile(): File? {
val mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_DCIM).let {
File(it, "[name_folder]").apply { mkdirs() } }
Log.v(TAG, "storage: $mediaStorageDir") ///storage/emulated/0/Android/data/[name_of_your_program]/files/DCIM/[name_folder]
if (!mediaStorageDir?.exists()!!){
if (!mediaStorageDir.mkdirs()) {
Log.v(TAG, "Failed to create directory")
return null
}
}
}
Hope that work for you :)
i started to use thumbnailator library to make thumbnail in a Spring Boot project
But i'm facing a problem when i try to delete the file, i got an exception telling me the file is being used by another process. I pretty new with Java and i can't figured out where does the problem might come from and what process should i stop/close:
File originalFile = mediaUtils.saveFile(pathOriginal, file);
String path = mediaUtils.resolvePath(imageDir, name, false, image.getCreation());
mediaUtils.saveJPG(originalFile, file.getContentType(), WIDTH_IMAGE_SIZE, path);
String pathThumb = mediaUtils.resolvePath(imageDir, name, true, image.getCreation());
mediaUtils.saveJPG(originalFile, file.getContentType(), WIDTH_IMAGE_SIZE_THUMB, pathThumb);
public File saveFile(String filePath, MultipartFile file) {
try {
Path path = Paths.get(getPath(filePath));
Files.createDirectories(path.getParent());
Files.copy(file.getInputStream(), path);
return new File(path.toString());
} catch (IOException e) {
LOG.error("could not save file", e);
throw new FileException("could not create file: " + getPath(filePath), e);
}
}
private void saveJPG(InputStream imageInputStream, File file, String contentType, int newWidth, String outputPath) {
try {
// verify it is an image
if (!Arrays.asList("image/png", "image/jpeg").contains(contentType)) {
throw new IllegalArgumentException("The file provided is not a valid image or is not supported (should be png or jpeg): " + contentType);
}
// Create input image
BufferedImage inputImage = ImageIO.read(imageInputStream);
newWidth = newWidth > inputImage.getWidth() ? inputImage.getWidth() : newWidth;
double ratio = (double) inputImage.getWidth() / (double) inputImage.getHeight();
int scaledHeight = (int) (newWidth / ratio);
Path path = Paths.get(baseUrl + outputPath + ".jpg");
Thumbnails.of(file)
.size(newWidth, scaledHeight)
.toFile(path.toFile());
LOG.info("writing image to {}", path);
} catch (IOException e) {
LOG.error("could not write image", e);
}
}
Thanks for any advice or help :)
You should make sure to close your inputstreams and files, after you've used them.
Otherwise things like you've mentioned, happen. A process does block your file.
So instead of using simple try-catch-blocks I would recommend to use try-with-resources which will close the underlying streams and files. For example:
try(InputStream imageInputStream = new FileInputStream(...)) {
// do your stuff
}
After the code in the brackets is done or an exception occured, the inputstream will be closed.
I have been struggling for two days to try and understand the process of copying a file to the SD card in Android. None of the methods I tried thus far seem to work.
My application has a Profile Picture setting. I need to launch an Intent to pick an Image, then I need to copy the Image to a new Path on the SD Card and then return the Uri of the new Image at which point I check the Images Orientation (Samsung Pics seem to be rotated 90 degrees sometimes). I then rotate the Image correctly and then save the Uri to a SharedPreferences File for use in the Application.
This is my Intent Call:
case R.id.ib_userImage:
i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
break;
This is my current horrific attempt at the copy function, I have changed it so much I am not very lost.
public static void copyImage(Context context, Uri uri) {
Log.i("ATTENTION", "Inside the Copy Function");
Log.i("ATTENTION", "Trying to copy file: " + uri.toString());
try {
String outputPath = Environment.getExternalStorageDirectory() + "/appname/images/";
File dir = new File(outputPath);
if(!dir.exists()) {
dir.mkdirs();
}
Log.i("ATTENTION", "Destination File Created at: " + dir.toURI().toString());
InputStream in = context.getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(dir);
byte[] buffer = new byte[1024];
while(in.read(buffer) > 0) {
out.write(buffer);
}
out.flush();
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("ATTENTION", "File Copied");
}
Thank you for the help, I will provide any other information you might need.
Update:
I am now getting the Following Exception During the Write Process
java.io.FileNotFoundException: /storage/emulated/0/appname/images: open failed: EISDIR (Is a Directory);
My Understaing is that I specified a Directory with the following code:
String outputPath = Environment.getExternalStorageDirectory() + "/medinfo/images/";
File dir = new File(outputPath);
if(!dir.exists()) {
dir.mkdirs();
}
and then passed it to the OutputStream:
OutputStream out = new FileOutputStream(dir);
and the OutputStream would create the file for me within that Directory.
I didn't think that was actually trying to open the Directory.
Usual problem. Don't ignore the count returned by read().
while ((count = in,read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
EDIT Your directory problem is cured by:
dir.getParentFile().mkdirs();
and removing the redundant existence check. At present you are creating the file itself as a directory.
I have a requirement where i have to save a file that i m generating using my java code and but as when i want to save it i want to let user decide where they want to save it.Like a download option that comes when we download a file from internet.I have tried using JFileChooser. But it does not work the way i want it to work.Can somebody please help.
Im creating the file like
try{
writer= new PrintWriter("F://map.txt", "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
JFrame parentFrame = new JFrame();
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
Writing to a file
Note that this will overwrite the file if it exists, and it will not automatically prompt you for sh*t if it does. You have to check if it exists by yourself.
byte dataToWrite[] = // source
FileOutputStream out = new FileOutputStream("the-file-name");
out.write(dataToWrite);
out.close();
In your case this would probably read out as
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(fileToSave.getPath());
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
finally {
if (in != null) in.close();
if (out != null) out.close();
}
}
Please note that this is untested code and I have no real routine with this stuff. You should google "java write file" or something like that if this proves to be erronous code :)
I have a program that takes a screenshot of my gui. It automatically saves the .gif file to the eclipse project directory. What I would like is to have it asking a user where to save the image. Basically so the user can browse the file directory and choose the directory.
Here's the code I have:
public void actionPerformed(ActionEvent event) {
try{
String fileName = JOptionPane.showInputDialog(null, "Save file",
null, 1);
if (!fileName.toLowerCase().endsWith(".gif")){
JOptionPane.showMessageDialog(null, "Error: file name must end with \".gif\".",
null, 1);
}
else{
BufferedImage image = new BufferedImage(panel2.getSize().width,
panel2.getSize().height, BufferedImage.TYPE_INT_RGB);
panel2.paint(image.createGraphics());
ImageIO.write(image, "gif", new File(fileName));
JOptionPane.showMessageDialog(null, "Screen captured successfully.",
null, 1);
}
}
catch(Exception e){}
I would use a file chooser dialog instead of a JOptionPane. Here is a link for the tutorial.
Example:
First of all you have to declare JFileChooser object in your class and initialize it.
public Class FileChooserExample{
JFileChooser fc;
FileChooserExample(...){
fc = new JFileChooser();// as a parameter you can put path to initial directory to open
...
}
Now create another method:
private String getWhereToSave(){
int retVal = fc.showSaveDialog(..);
if(retVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
return file.getAbsolutePath();
}
return null;
}
This method returns to you the absolute path which user selected. retVal indicates which button was pressed (Save or Cancel). And if it was pressed Save then you handle the selected file.
Then you have this method you can incorporate this with your code. Instead of this line:
String fileName = JOptionPane.showInputDialog(null, "Save file", null, 1);
Write:
String fileName = getWhereToSave();