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

时间:2015-08-19 15:18:08

标签: android background byte android-imagebutton

我需要上传从图库中挑选的解析图片。我使用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();

任何人都知道如何做到这一点?

提前致谢,

1 个答案:

答案 0 :(得分:0)

首先,您在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);