Question

hey i want to load 2 images in 1 activity like a compare something if i click button1 I can load image and set image to ImageView 1 and if i click on button 2 i can load but not able to set to imagview2 why? Where I am doing wrong?

check my code there are wrong

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.push_left_in, R.anim.hold);
    setContentView(R.layout.compare);

    ImageButton imgBtnCpr1 = (ImageButton) findViewById(R.id.imgBtnCpr1);

    imgBtnCpr1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE1);

        }
    });

    ImageButton imgBtnCpr2 = (ImageButton) findViewById(R.id.imgBtnCpr2);

    imgBtnCpr2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent t = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(t, RESULT_LOAD_IMAGE2);
        }
    });
}

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

    if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageViewBrw1 = (ImageView) findViewById(R.id.imgViewBrw1);
        imageViewBrw1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        // -----------------------------------------------------------------------------------//

        if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage2 = data.getData();
            String[] filePathColumn2 = { MediaStore.Images.Media.DATA };

            Cursor cursor2 = getContentResolver().query(selectedImage2,
                    filePathColumn2, null, null, null);
            cursor2.moveToFirst();

            int columnIndex2 = cursor2.getColumnIndex(filePathColumn2[0]);
            String picturePath2 = cursor2.getString(columnIndex2);
            cursor2.close();

            ImageView imageViewBrw2 = (ImageView) findViewById(R.id.imgViewBrw2);
            imageViewBrw2.setImageBitmap(BitmapFactory
                    .decodeFile(picturePath2));
        }

    }
}
Was it helpful?

Solution

Change your code like this

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

    if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageViewBrw1 = (ImageView) findViewById(R.id.imgViewBrw1);
        imageViewBrw1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
    // -----------------------------------------------------------------------------------//

    if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage2 = data.getData();
            String[] filePathColumn2 = { MediaStore.Images.Media.DATA };

            Cursor cursor2 = getContentResolver().query(selectedImage2,
                    filePathColumn2, null, null, null);
            cursor2.moveToFirst();

            int columnIndex2 = cursor2.getColumnIndex(filePathColumn2[0]);
            String picturePath2 = cursor2.getString(columnIndex2);
            cursor2.close();

            ImageView imageViewBrw2 = (ImageView) findViewById(R.id.imgViewBrw2);
            imageViewBrw2.setImageBitmap(BitmapFactory
                    .decodeFile(picturePath2));
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top