Nicky Niq
Nicky Niq

Reputation: 1

How to send Image from Imageview to a Server

I am making SignUp form which includes many fields along with Profile Picture. I have converted everything to a string except the Image. I am unable to understand that How can I send Image to server along with other string values when someone clicks on the SignUp button.

Following is my source code for your perusal:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class SignUp extends Activity {
    ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);


        imageView = (ImageView) findViewById(R.id.imagecontact);
    }
    public void onGalleryClick(View view){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,ACCESSIBILITY_SERVICE),1);
    }
    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        if(resultCode == RESULT_OK){
            if(requestCode == 1)
                imageView.setImageURI(data.getData());
        }
    }


    public void onSignUpClick(View view) {
        if (view.getId() == R.id.BT_signup) {
            EditText name = (EditText) findViewById(R.id.ED_bakeryname);
            EditText maail = (EditText) findViewById(R.id.ED_emailaddress);
            EditText number = (EditText) findViewById(R.id.ED_mobilenumber);
            EditText address = (EditText) findViewById(R.id.ED_postaladd);
            EditText pass1 = (EditText) findViewById(R.id.ED_pass1);
            EditText pass2 = (EditText) findViewById(R.id.ED_pass2);
            ImageView imageView1 =(ImageView)view.findViewById(R.id.imagecontact);


            String namestring = name.getText().toString();
            String mailstring = name.getText().toString();
            String numberstring = name.getText().toString();
            String addressstring = name.getText().toString();
            String pass1string = name.getText().toString();
            String pass2string = name.getText().toString();


            if (!pass1string.equals(pass2string)) {
                //popup msg

                Toast pword = Toast.makeText(SignUp.this, "Passwords don't match!", Toast.LENGTH_SHORT);
                pword.show();
            }
        }

    }


}

Upvotes: 0

Views: 921

Answers (1)

Steve Kamau
Steve Kamau

Reputation: 2785

I am assuming you are trying to convert your image into base64 String.

From your code, you can first get the image from imageView:

BitmapDrawable drawable = (BitmapDrawable) imageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();

Then convert the bitmap to a byte array:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();

Then you can get the string:

String image_byte=String.valueOf(byte[] byteArray);

Then ofcourse you can jus pass the string like the rest of the other detail. Then afterwards you can get the string back from the server and decode it into an imageView.

Upvotes: 1

Related Questions