0

嘿,我想在 1 个活动中加载 2 个图像,例如比较某些东西,如果我单击 button1 我可以加载图像并将图像设置为 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 回答