0

ボタン1をクリックすると、何かを比較するような1つのアクティビティで2つの画像をロードしたいのですが、画像をロードしてImageView 1に設定できます。ボタン2をクリックすると、ロードできますが、imagview2に設定できません。私はどこで間違っていますか?

コードが間違っていることを確認してください

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

    }
}
4

1 に答える 1

0

このようにコードを変更します

   @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));
        }
}
于 2013-07-25T03:28:42.177 に答える