0

I have captured image from android and passed it to the database side using arraylist in the string form. Now the problem is how to convert this string to BLOB and save it in the database using servlets.

Thank you guys for the help

Here is the android code: new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    ArrayList<NameValuePair> postparameters=new ArrayList<NameValuePair>();
                    postparameters.add(new BasicNameValuePair("profile",image.toString()));

                    String response=null;
                    try{
                        response=RegisterHttpReq.executeHttpPost("http://10.0.2.2:8080/epass_admin/storeImage", postparameters);
                        String res = response.toString();
                        System.out.println(res);
                        String resp = res.replaceAll("\\s+", "");
                    }catch(Exception e){}
                }
            }).start();

             try{
                Toast.makeText(getApplicationContext(), "Image uploaded successfully", Toast.LENGTH_LONG).show();

                Intent in=new Intent(getApplicationContext(),Imageset.class);
                image=imv.getDrawingCache();

                 Bundle extras = new Bundle();
                 extras.putParcelable("imagebitmap", image);
                 in.putExtras(extras);
                 startActivity(in);
             }catch(Exception e){}
        }
    });
Omor Faruq
  • 29
  • 5

1 Answers1

0

As per Wikipedia "A Binary Large OBject (BLOB) is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia objects". That does not exclude the possibility to store plain text in a BLOB field. That said, I am not sure toString() will convert your image to a string. You should convert your image to base64 and back from base64 to binary. See Java - Convert image to Base64. Once you get your binary data back, you can insert it in the database. You could also store the image base64 encoded and only decode it upon retrieval. That would consume tipically 30% extra space though.

Code to insert the image BLOB data (http://www.java2s.com/Code/Java/Database-SQL-JDBC/InsertpicturetoMySQL.htm):

/*

Defining the Table: Oracle and MySql

create table MyPictures (
   id INT PRIMARY KEY,
   name VARCHAR(0),
   photo BLOB
);
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertPictureToMySql {
  public static void main(String[] args) throws Exception, IOException, SQLException {
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
    String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)";

    FileInputStream fis = null;
    PreparedStatement ps = null;
    try {
      conn.setAutoCommit(false);
      File file = new File("myPhoto.png");
      fis = new FileInputStream(file);
      ps = conn.prepareStatement(INSERT_PICTURE);
      ps.setString(1, "001");
      ps.setString(2, "name");
      ps.setBinaryStream(3, fis, (int) file.length());
      ps.executeUpdate();
      conn.commit();
    } finally {
      ps.close();
      fis.close();
    }
  }
}
Community
  • 1
  • 1
Tarik
  • 10,810
  • 2
  • 26
  • 40