views:

74

answers:

3

I am currently working on an application named BlackBerry Contacts Sync and I am stuck at this point on how to sync Contact's Image. How can images be stored in the database (MySQL) and how to retrieve them?

A: 

You can store the images in a blob field after you base64 encode them.

Glen Morgan
A: 

In case of PHP you can upload multipart file over HTTP Post and then save this image to MySQL, and retrieve it later.

Also you can use .NET or Tomcat web services with JAX-RPC.

Max Gontar
thanks a lot for your help.
Farhan
you're welcome!
Max Gontar
A: 

If you want to store your images in the database instead of just a path to reference the image, the column that will hold your image need to be of BLOB type.

CREATE TABLE upload (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(30) NOT NULL,
    content LONGBLOB NOT NULL,
    PRIMARY KEY(id)
);

From your BlackBerry program (warning, untested):

//First param is a Bitmap, second is quality
JPEGEncodedImage encodedImage = JPEGEncodedImage.encode(image, 100);
byte imageBytes[]=encodedImage.getData();

//The HTTP connection stuff
HttpConnection conn = (HttpConnection) Connector.open(SERVER_URL, Connector.READ_WRITE);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(imageBytes.length));
//Change this part to what your server side code expects
String contDisp="Content-Disposition:form-data; uploadedimage=YOUR_IMAGE_NAME;filename=\"Image.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type="Content-Type:image/jpeg";

OutputStream out= conn.openOutputStream();
out.write(imageBytes);
out.flush();
out.close();

So basically you use HttpConnection to setup the headers for a POST request, with multipart form data as content type. There are other headers that may be needed, to know exactly what you need (not BlackBerry specific) check the HTTP/1.1 protocol specification.

How to actually store the image into the database will depend on your server side implementation. This can be anything from PHP, ASP, JSP or any server side technology that allows you to talk to your MySQL instance.

Cesar
Sir please provide me this Solution also that how i can download image from PHP as i am using <img> tag in php to display image but it is not accessed in JDE to get image from it. When i use readfile function then Image is read but other Contacts Information is not accessed in JDE. I am using MySql under PHPMyAdmin.
Farhan