aspose file tools*
The moose likes Java in General and the fly likes getblob from db, then convert to byte and write to bitmap/jpeg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "getblob from db, then convert to byte and write to bitmap/jpeg" Watch "getblob from db, then convert to byte and write to bitmap/jpeg" New topic
Author

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

charles wong
Greenhorn

Joined: Oct 26, 2010
Posts: 6
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

Joined: Oct 26, 2010
Posts: 6
by the way , there is not connection issue to this mssql fyi
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Oct 26, 2010
Posts: 6


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

Joined: Feb 02, 2009
Posts: 214
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

Joined: Oct 26, 2010
Posts: 6
Sorry still giving me im is null exception error
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
charles wong
Greenhorn

Joined: Oct 26, 2010
Posts: 6
Actually the DB store ole data blob, I guess I need to translate it into bitmap any Java code for tis
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getblob from db, then convert to byte and write to bitmap/jpeg