• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

getblob from db, then convert to byte and write to bitmap/jpeg

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having issue with writing to a bitmap and jpeg on disk , i have the following code which extract blob from mssql, then i convert it to byte[], after that i try to output it to bmp/jpeg but the system give me error "Exception :java.lang.IllegalArgumentException: im == null!" and the bmp/jpeg file created are all empty with 0 size on disk, please help me guru, thank you


Class.forName(driverName);

con = DriverManager.getConnection(url,userName,password);

stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT photo FROM xxt Where xx_id='N7V9FS3005421993' and photo is not null");
while (rs.next()) {

Blob test=rs.getBlob("photo");
int blobLength = (int) test.length();
byte[] blobAsBytes = test.getBytes(1, blobLength);


BufferedImage image = ImageIO.read( new ByteArrayInputStream( blobAsBytes ) );


ImageIO.write(image, "JPEG", new File("C:\\sams1\\image.JPG"));
ImageIO.write(image, "JPEG", new File("C:\\sams1\\image.BMP"));
 
charles wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way , there is not connection issue to this mssql fyi
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post all code. What you posted here does not compile. You may have left out parts which are responsible for the exception...if we don't know the details, we can't help you properly.

That said. First check the exception stacktrace. It should show you a line number and class where the exception happened, check if you wrote something wrong in that line. Also, check the results of your database calls! Is the result length == 0? Do you use an empty (or null) array? Log the values you get, to have more information available.
 
charles wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


System.out.println("Length"+blobLength);System.out.println("testlen"+blobAsBytes.length); both of the system.out return the size but when arrive at ImageIO.write(image, "JPEG", new File("C:\\sams1\\image.JPG"));, it gives im=null exception error , and the image it produce in the sams1 folder is zero size and empty view
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently then the BufferedImage returned by ImageIO.read() is null. I haven't used that API before, maybe it is better/neccessary to create an ImageInputStream with your byte data and use that for ImageIO.read()? Something like

And again, test if the image is null before using it
 
charles wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry still giving me im is null exception error
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Javadocs of all ImageIO.read methods:

If no registered ImageReader claims to be able to read the resulting stream, null is returned.


Apparently there's something wrong with your image data. If you save blobAsBytes to a file directly and then try to open it in any image editing / viewing software*, does that succeed?

* most programs look at the actual content, not the file name, so that shouldn't be a problem.
 
charles wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the DB store ole data blob, I guess I need to translate it into bitmap any Java code for tis
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic