0

I am unable to upload two separate image in Two Imageview using onActivityResult. Here when I Select ImageView one and Cope Image then image is set in Imageview. But when I select Imageview Two and it opens Gallery and When I crop image and press Ok then Imageview one gets replaced it's previous image instead that Imageview two should have the selected image and Imageview one should have same image previously selected

My Activity is

import java.util.Random;

public class UploadBook extends AppCompatActivity {

    ImageView iv1,iv2;
   private static final int CAMERA_REQUEST_CODE_two=2;
    private static final int RESULT_CODE_TWO=2;
    private static final int CROP_IMAGE_ACTIVITY_REQUEST_CODE_TWO=2;
    private static final int CAMERA_REQUEST_CODE = 1;

    Uri filePath = null;
     Uri imagePath = null;
    public Books b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_book_request);
      

        iv1 = (ImageView) findViewById(R.id.itemImage1);
          iv2 = (ImageView) findViewById(R.id.itemImage2);

      

        imageoneButtonclick();
         imagetwoButtonclick();
        


    void imageoneButtonclick() {
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImage.activity(filePath).setGuidelines(CropImageView.Guidelines.ON)
                        .setAspectRatio(1,1).start(UploadBook.this);

            
            }
        });
    }

        void imagetwoButtonclick() {
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImage.activity(imagePath).setGuidelines(CropImageView.Guidelines.ON)
                        .setAspectRatio(1,1).start(UploadBook.this);

              
            }
        });
    }

   

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
            filePath = data.getData();
            iv1.setImageURI(filePath);
      }

         if(requestCode == CAMERA_REQUEST_CODE_two && resultCode == RESULT_CODE_TWO) {
            imagePath = data.getData();
            iv2.setImageURI( imagePath);
      }
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri resultoneUri = result.getUri();

                iv1.setImageURI(resultoneUri);
                filePath = resultoneUri;

            }
             if (resultCode == RESULT_CODE_TWO) {
                Uri resultUri = result.getUri();

                iv2.setImageURI(resultUri);
                imagePath = resultUri;

            }
              else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
        }



            }
        }


     
}

Please Help Thanks in advance.

Blueyeti
  • 43
  • 8
  • What are the numerical values of `CAMERA_REQUEST_CODE_one` and `CAMERA_REQUEST_CODE_`? They need to be different. – Mike M. Oct 22 '18 at 00:52
  • I have CAMERA_REQUEST_one = 1; and CAMERA_REQUEST_CODE_=2; But still when I click imageview and crop image and other imageview is also replaced with same image...!! But I want to pick image different image one after another. – Blueyeti Oct 22 '18 at 01:12
  • I'm not exactly sure what you mean by that, nor am I sure which part isn't working for you; camera or crop. However, I do see a few things that don't seem to be what you want. In the camera section, you have an extra `}`, so it's not clear how that's actually grouped. Also, you're setting `filePath` on both `iv1` and `iv2`, and `ImagePath` and `twodata` aren't defined anywhere, so we don't know what's going on there. In the crop section, you're again setting the same thing on both `ImageView`s. You'll need two separate request codes for that, too, just like you already have for the camera. – Mike M. Oct 22 '18 at 01:21
  • why there are comments on Intent of each button click? – Asad Ali Oct 25 '18 at 15:25
  • @AsadAli Ok I have edited out my comments. – Blueyeti Oct 25 '18 at 15:41
  • have you check what is "resultcode" value in onActivityResult() on each imageview click – Asad Ali Oct 25 '18 at 15:45
  • where can I check it from logcat?? @AsadAli – Blueyeti Oct 25 '18 at 16:28
  • use break point to check – Asad Ali Oct 25 '18 at 17:47
  • @AsadAli I did and it returns request Cod = 203 and resultCode = -1 in both cases..How can I solve this? – Blueyeti Oct 26 '18 at 01:15
  • its because you are not passing the request codes with intents when opening gallery – Asad Ali Oct 26 '18 at 05:10
  • take a look at this post https://stackoverflow.com/questions/38352148/get-image-from-the-gallery-and-show-in-imageview – Asad Ali Oct 26 '18 at 05:12

0 Answers0