1

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.

1 Answer 1

2

To convert image to string, use following short of code.

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//this will convert image to byte[] 
byte[] byteArrayImage = baos.toByteArray(); 
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

Now, you have encodedImage string of image.

Your code of "saveItem()" looks like following.

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);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    //this will convert image to byte[] 
    byte[] byteArrayImage = baos.toByteArray(); 
    // this will convert byte[] to string
    String encodedImage = Base64.encodeToString(byteArrayImage, 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();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

replace "String ncodedImage = Base64.encodeToString(blob,Base64.DEFAULT);" lines with my answer's 4 lines.
in my case, youbitmap is yourSelectedImage ..?
hm..i can see the log this "Log.v("log_tag", System.currentTimeMillis()+".jpg");" but, after that, no reaction is happened..
@hurj, yes, yourbitmap means image you want to send on server. May be your code to send on server not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.