0

I have a button to select a picture or take a picture from camera, what i want is when the imageview that i use to attached the picture has an image on it., show a message right away, without clicking a button.

What i do now is when you press on a button then it shows some hidden edittext that says "image uploaded" or something. I want to achive this but without clicking any button. I've had tried many other options but all of them use a button.

Any samples or ideas! Thanks in advance

bb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            validacionImagen();
  }
});

METHOD THAT VALIDATES WHETER THERE'S AN ATTACHED IMAGE:

     public void validacionImagen(){
    if(imageView.getDrawable()==null){
        buttonChoose.setError("Sube una fotografía");
        buttonChoose.setFocusable(true);
        buttonChoose.setFocusableInTouchMode(true);
        buttonChoose.requestFocus();
    }else{
        imagencargada.setVisibility(View.VISIBLE);
        textoimagencargada.setVisibility(View.VISIBLE);
        buttonChoose.setError(null);

    }
}

PICTURE CODE:

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Elige una imagen"), PICK_IMAGE_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAM_REQUEST){
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
        //imageView.setImageBitmap(bitmap);
    }else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Daniel Prado
  • 79
  • 11

2 Answers2

0

You can try to make something like this:

public class ImageView extends ImageView {

    private OnImageChangeListener onImageChangeListener;

    public ImageView(Context context) {
        super(context);
    }

    public ImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setImageChangeListiner(
            OnImageChangeListener onImageChangeListener) {
        this.onImageChangeListener = onImageChangeListener;
    }

    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        if (onImageChangeListener != null)
            onImageChangeListener.onImageChanged(this);
    }


    @Override
    public void setBackgroundDrawable(Drawable background) {
        super.setBackgroundDrawable(background);
        if (onImageChangeListener != null)
            onImageChangeListener.onImageChanged(this);
    }


    public interface OnImageChangeListener {
        void onImageChanged(ImageView imageView);
    }

}

Create custom ImageView with a listener to notify when you set the image and override some method that you use to set the image in ImageView to call the listener.

Daniel RL
  • 353
  • 1
  • 12
0

If you're calling startActivityForResult in order to get the picture from the camera or gallery like:

startActivityForResult(pictureIntent, SELECT_PICTURE);

Then, you can override onActivityResult to check the results from that intent.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case SELECT_PICTURE:
            if(resultCode == RESULT_OK && data != null) {
                //Manage the picture taken here with data
            } else {
                //The intent was cancelled or didn't bring any data
            }
    }
}

Now, if this isn't the approach you're looking for and all you want to do is to check when an ImageView has changed, you can implement a custom ImageView with a listener. Refer to this

FerDensetsu
  • 736
  • 5
  • 20