如何将Imageview中的Image发送到服务器

时间:2016-05-01 09:21:58

标签: java android

我正在制作SignUp表单,其中包含许多字段以及Profile Picture。我已将所有内容转换为除Image之外的字符串。我无法理解当有人点击SignUp按钮时,如何将Image连同其他字符串值一起发送到服务器。

以下是我的源代码供您阅读:

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();
            }
        }

    }


}

1 个答案:

答案 0 :(得分:0)

我假设您正在尝试将图像转换为base64字符串。

从您的代码中,您可以先从imageView获取图像:



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




然后将位图转换为字节数组:



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




然后你可以得到字符串:



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




然后你可以像其他细节一样传递字符串。然后,您可以从服务器返回字符串并将其解码为imageView。