This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes directly saving an image to a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "directly saving an image to a file?" Watch "directly saving an image to a file?" New topic
Author

directly saving an image to a file?

Kyle Rayner
Greenhorn

Joined: Mar 22, 2006
Posts: 1
Greetings. I already managed to acquire an image by using the BufferedImage class - and with some fiddling I found out how I can save it to a file, BUT...

...the thing is, I'd like the image to be saved without being recompressed so that the resulting image file is a 1:1 copy of what I started with.

If someone says "why not just copy it then" - I'm retrieving the image from the web, so I can't just copy it. (erh... or can I?)

Kyle
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Here is one way of doing it.



You can try it with

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"Kyle", please take a look at our display name policy and then edit your display name to one less obviously fictitious (to some of us, at least.). Thank you.


"I'm not back." - Bill Harding, Twister
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I don't agree with all that Keith wrote here -- buffering both input and output, then providing additional buffers and copying over and over again, but take a look at the code for copying from one file to another, from the Java Almanac::(I think you could do better by closing the streams in a finally block.) Given that url.openStream() returns an InputStream, all that needs to be added is q.e.d.


There is no emoticon for what I am feeling!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

If you already have an instance of BufferedImage, you can save it directly with the static write() method of javax.imageio.ImageIO


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Joe Ess:
If you already have an instance of BufferedImage, you can save it directly with the static write() method of javax.imageio.ImageIO


I think what the OP really wanted to do was copy from www.acme.com/coyote.gif to a local file xxx.gif. Converting the file back and forth to a BufferedImage is asking for trouble (apart from efficiency questions) -- what if you want to preserve the file's image format (woops, where's my gif writer?) and compression level, if any? Did you copy the file's metadata? Etc, etc...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: directly saving an image to a file?
 
Similar Threads
Moving files from server A to server B using JSch
Copy Picture (Image)
Drawing a Transparent PNG
how does JSP get an image created from a servlet ?
images inserted into JTextPane