Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert byte array to blob

I want to fetch an image from database. For that I have created a byte array for an image, which is passed by a string, and now I want to convert that string into image format. I am assigning that image to a Jlabel field. The code is as follows:

try {
    Blob image_vis = rs1.getBlob(10);
    InputStream x=image_vis.getBinaryStream();
    OutputStream out=new FileOutputStream(string_op);
    byte[] bytes = string_op.getBytes();
    String s=new String(bytes);
    System.out.println(+s);  //prints bytes for the string
    ImageIcon icon_cap = new ImageIcon(string_op);
    image_cap.setIcon(icon_cap);   //prints nothing to Jlabel
    //image_cap.setText(s);     //prints a path of a image
}
like image 942
Anjali Avatar asked Jun 01 '12 12:06

Anjali


People also ask

How to convert arraybuffer to blob in JavaScript?

Convert an ArrayBuffer or typed array to a Blob var array = new Uint8Array([0x04, 0x06, 0x07, 0x08]); var blob = new Blob([array]); PDF - Download JavaScript for free

How to get bytes from a blob in MySQL?

In MySQL blob has a method: getBytes () This method Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. Blob myblob= my_result_set.getBlob ("Database_Field"); int myblobLength = (int) myblob.length (); byte [] myblobAsBytes = myblob.getBytes (1, myblobLength); blob.free ();

How to convert a byte array to a displayable image?

If it is an image then you can use Image to convert the byte array to the displayable image, for Winforms. If you just have the byte array then put it into a MemoryStream and then use FromStream to read it. If it is on disk already then use FromFile instead.

What is blob free () method in Java?

This method Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. free () method frees the blob object and free up the resources that was hold by it. I want to give you an another easy example so that you can get a idea on how to implement it. Assume that you are using these:


1 Answers

Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes);
like image 115
Pablo Avatar answered Sep 26 '22 01:09

Pablo