在onActivityResult

时间:2018-10-21 17:40:16

标签: java android android-activity onactivityresult

我无法使用onActivityResult在“两个Imageview”中上传两个单独的图像。 在这里,当我选择一个ImageView并处理Image时,将在Imageview中设置图像。 但是,当我选择Imageview Two并打开Gallery时,当我裁剪图像并按OK时,Imageview One被替换为先前的图像,而不是Imageview 2应该具有所选图像,而Imageview one应该具有先前选择的相同图像

我的活动是

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();
        }



            }
        }


     
}

请帮助 预先感谢。

0 个答案:

没有答案