Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
charles wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
by the way , there is not connection issue to this mssql fyi
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Sorry still giving me im is null exception error
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic