I am working on client-server communication. Users can select an image from gallery. Selected images would be saved in two places, local DB and server DB. If users save it, the selected image would be saved into local database with path(String) and also should be saved into Server database. The problem is I don't know how to get images byte array to encode to String to pass the image to server side.
Local DB : Image -> Path(String) (This is done) Server DB : Image -> Byte -> String -> Send to server
Here is the code..
Bundle extras = getIntent().getExtras();
if (extras != null) {
id=extras.getInt("id");
inputname.setText(extras.getString("name"));
inputnote.setText(extras.getString("note"));
image = extras.getString("blob");
//Convert image into string to save path in local DB
BitmapFactory.Options op=new BitmapFactory.Options();
op.inSampleSize=8;
yourSelectedImage = BitmapFactory.decodeFile(image, op);
inputphoto.setImageBitmap(yourSelectedImage);
}
How to set blob in the saveItem method..?
private void saveItem() {
// Client-Server - Start //////////////////////////////////////
String name = inputname.getText().toString();
String description = inputnote.getText().toString();
// Encode the image file to String !! by using Base64
String encodedImage = Base64.encodeToString(blob, Base64.DEFAULT);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("name", name));
params1.add(new BasicNameValuePair("description", description));
params1.add(new BasicNameValuePair("photo",encodedImage));
Log.v("log_tag", System.currentTimeMillis()+".jpg");
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params1);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
Log.v("log_tag", "In the try Loop" );
if (success == 1) {
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
Thank you in advance.