在图像按钮中选择图像作为背景并转换为字节

picked image as background in image button and convert to byte

我需要上传从图库中挑选的经过解析的图像。我使用 imageButton 来调用 de intent 并显示选择的图像,这是有效的。

public class Datos extends Activity implements OnItemSelectedListener {


private final int SELECT_PHOTO = 1;
private ImageButton imageView;
byte [] data;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_datos);
    btnClick();

ImageButton pickImage = (ImageButton) findViewById(R.id.imageButton);
    pickImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                try {
                    Uri imageUri = imageReturnedIntent.getData();
                    InputStream imageStream = getContentResolver().openInputStream(imageUri);
                    Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    imageView.setImageBitmap(selectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] data = stream.toByteArray();

                    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), selectedImage);
                    imageView.setBackgroundDrawable(bitmapDrawable);



                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }


            }
    }
}

 public void btnClick() {

    Button buttonEnviar = (Button) findViewById(R.id.enviar);


    buttonEnviar.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {

            //Storing image in parse passed in  onclick method of a button with the below code:



            Intent intentDatos = new Intent(Datos.this, Inicio.class);
            startActivity(intentDatos);

            ParseObject testObject = new ParseObject("Musica");


            //data = "".getBytes();
            //byte[] data = stream.toByteArray();
            ParseFile file = new ParseFile("selected.png", data);

            testObject.put("imagen", file);

            testObject.saveInBackground();

使用这段代码,我在按钮中显示了选定的图像,但它没有替换背景。我不知道如何使用所选图像更改背景。

此外,在接下来的代码中,我将背景转换为文件,我可以上传它进行解析,但我需要转换选取的图像,

byte[] data = "".getBytes();

有人知道怎么做吗?

提前致谢,

首先,selectedImage 中有位图,因此您可以像这样将其转换为字节数组:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();

其次,您可以像这样设置图像按钮的背景:

BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), selectedImage);
 pickImage.setBackgroundDrawable(bitmapDrawable);