我有两个imageview,但是当我调用一个imageview设置图像时,将它们设置为相同的图像

时间:2018-07-17 17:32:16

标签: java android xml

我有两个背对背的图像视图。当我在第一个图像视图上设置图像时,它也在下一个图像视图上设置相同的图像,并且当我从一个图像视图更改图像时,它也从另一个图像视图更改了图像。如何修复它。请帮我。先感谢您。 相关代码是:

`    ImageView cpic,psymbol;
     private static final int PICK_IMAGE=100;
     private static final int SET_IMAGE=100;
     Uri imageUri,simageUri;
     cpic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallery();
        }
    });
    psymbol.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallary();
        }


    });

}

private void openGallery() {
    Intent Gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(Gallery,PICK_IMAGE);

}
private void openGallary(){
    Intent Gallary = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(Gallary,SET_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode==RESULT_OK && requestCode==PICK_IMAGE){
        imageUri=data.getData();



       InputStream imageinput = null;
        try {
            imageinput=getContentResolver().openInputStream(imageUri);
            cpic.setImageBitmap(BitmapFactory.decodeStream(imageinput));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }



    }
    if (resultCode==RESULT_OK && requestCode==SET_IMAGE){
        simageUri=data.getData();

        InputStream inputimage = null;
        try {
            inputimage=getContentResolver().openInputStream(simageUri);
            psymbol.setImageBitmap(BitmapFactory.decodeStream(inputimage));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}

`

2 个答案:

答案 0 :(得分:0)

private static final int PICK_IMAGE=100;
private static final int SET_IMAGE=100;

您正在为他们两个使用相同的请求代码。因此,您必须使用其他请求代码。

因此,您应该这样做-

private static final int PICK_IMAGE=100;
private static final int SET_IMAGE=101;


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

    if (resultCode==RESULT_OK && requestCode==PICK_IMAGE){
        imageUri=data.getData();

        InputStream imageinput = null;
        try {
            imageinput=getContentResolver().openInputStream(imageUri);
            cpic.setImageBitmap(BitmapFactory.decodeStream(imageinput));
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    if (resultCode==RESULT_OK && requestCode==SET_IMAGE){
        simageUri=data.getData();

        InputStream inputimage = null;
        try {
            inputimage=getContentResolver().openInputStream(simageUri);
            psymbol.setImageBitmap(BitmapFactory.decodeStream(inputimage));

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

这是您的问题:

 private static final int PICK_IMAGE=100;
 private static final int SET_IMAGE=100;

更改这些变量之一的值,因为在onActivityResult中同时执行了两个if语句。