0

The initial goal of the buttons is to allow the user to save an image into an imageviewer. This works, but the method I used stores the images into an imageviewer.

It will not allow me to save the image into another imageviewer when another button is pressed.

I was thinking of using an if statement:

if (button is pressed) {
    code runs
} else if (button pressed) {
    code runs
} ...

but I do not know how to do this for Android.

can anyone help me on this please.

public class gallery extends AppCompatActivity  {
    ImageView image1;
    Integer Ask_for_camera = 2, file = 1;
    Button btnimage1;
    Button btnimage2;
    EditText ettext1;
    private FirebaseAuth auth;
    private FirebaseDatabase firebaseDatabase;
    private DatabaseReference gallerytext;
    private HashMap<String,String>savedtext;
    Button btntext1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        image1 = (ImageView) findViewById(R.id.image1);
        btnimage1 = (Button) findViewById(R.id.btnimage1);
        btnimage2 = (Button) findViewById(R.id.btnimage2);
        ettext1 = (EditText) findViewById(R.id.ettext1);
        btntext1 = (Button) findViewById(R.id.btntext1);
        btntext1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                savetext();
            }
        });
        btnimage1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                image1();
            }
        });
        btnimage2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                image1();
            }    
        });
        initialise();    
    }

    public void savetext() {
        savedtext = new HashMap<String,String>();
        savedtext.put("gallerytext", ettext1.getText().toString());
        gallerytext.child(auth.getCurrentUser().getUid()).setValue(savedtext);
    }

    public void initialise() {
        auth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        gallerytext = firebaseDatabase.getReference("savedtext");
    }

    private void image1() {
        final CharSequence[] items = {"Camera", "From Gallery", "Cancel"};
        AlertDialog.Builder Builder = new AlertDialog.Builder(gallery.this);
        Builder.setTitle("Click to add image");

        Builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int option) {
                if (items[option].equals("Camera")) {
                    Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(in, Ask_for_camera);

                } else if (items[option].equals("From Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/");
                    startActivityForResult(intent.createChooser(intent, "Select a File"), file);

                } else if (items[option].equals("Cancel")){
                    dialogInterface.dismiss();
                }
            }
        });
        Builder.show();    
    }

    @Override
    public void onActivityResult(int requestcode, int resultcode, Intent data) {
        super.onActivityResult(requestcode, resultcode, data);

        if (resultcode == Activity.RESULT_OK) {

            if (requestcode == Ask_for_camera) {    
                Bundle b = data.getExtras();
                final Bitmap bm = (Bitmap) b.get("data");
                image1.setImageBitmap(bm);    
            } else if (requestcode == file) {    
                Uri imageselected = data.getData();
                image1.setImageURI(imageselected);
            }    
        }
    }

}
paulina_glab
  • 2,467
  • 2
  • 16
  • 25

1 Answers1

0

Declare two boolean variable before onCreate():

private boolean isButton1Clcik= false;
private boolean isButton2Clcik= false;

Now update variable according to button click():

btnimage1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
isButton1Clcik= true;            
image1();
        }
    });

  btnimage2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view){
isButton2Clcik=true;
            image1();
        }

    });

Replace onActivityResult with this code :

@Override
public void onActivityResult(int requestcode, int resultcode, Intent data){
    super.onActivityResult(requestcode, resultcode, data);



    if (resultcode == Activity.RESULT_OK) {
       if(isButton1Clcik){

        if (requestcode == Ask_for_camera){

            Bundle b = data.getExtras();
            final Bitmap bm = (Bitmap) b.get("data");
            image1.setImageBitmap(bm);

        }else if (requestcode == file){

            Uri imageselected = data.getData();
            image1.setImageURI(imageselected);
        }
        isButton1Clcik=false;
     } else if(isButton2Clcik){
        if (requestcode == Ask_for_camera){

            Bundle b = data.getExtras();
            final Bitmap bm = (Bitmap) b.get("data");
            image2.setImageBitmap(bm);

        }else if (requestcode == file){

            Uri imageselected = data.getData();
            image2.setImageURI(imageselected);
        } 

          isButton2Clcik=false;
     }//end of check Button 2 click
  }//end of check resultCode

} 

Note: image2 is object of 2nd image view there no image2 in your code!

Wajid khan
  • 842
  • 9
  • 18