图像视图:第二个图像被捕获的第一个图像替换

时间:2015-12-24 08:51:13

标签: android imageview onactivityresult android-afilechooser

你好,我在这个编码中受到打击首先我有两个图像视图当我们点击其中一个时,选择器对话框将打开,其中有两个选项1.take photo 2.select from gallery when我从厨房中选择一切都正常,问题出在哪里,

当我们选择拍摄照片选项时,在我们为第一张图像设置其预览后拍摄图像时,其工作正常,而此处的问题出现在第二张图片中。当我们选择拍摄照片选项并在拍摄照片后,它正在用第二张图像替换第一张图像的图像。

这是Java代码:

public class PrescriptionUpload extends ActionBarActivity {
    ImageView uploadimage1, uploadimage2;
    Bitmap imagemap, imagemapsec;
    int imageupload = 1;
    int imagesecpload = 2;
    int requestcamera = 0;
    Button uploadpric;
    StringBuilder sb;
    String uploadimg2;
    Toolbar include2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prescriptionimages);
        include2= (Toolbar) findViewById(R.id.include2);
        setSupportActionBar(include2);
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        uploadimage1 = (ImageView) findViewById(R.id.uploadimage1);
        uploadimage1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectimage(imageupload);
            }
        });
        uploadimage2 = (ImageView) findViewById(R.id.uploadimage2);
        uploadimage2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectimage(imagesecpload);
            }
        });
    }



    private void selectimage(final int number) {
        final CharSequence[] items = {"Take Photo", "Choose from Gallery"};
        AlertDialog.Builder builder = new AlertDialog.Builder(PrescriptionUpload.this);
        builder.setTitle("Add Prescription");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, requestcamera);
                } else if (items[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            number);
                }
            }
        });
        builder.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            if (requestCode == imageupload) {
                try {
                    imagemap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                    uploadimage1.setImageBitmap(imagemap);
                    uploadimage2.setVisibility(View.VISIBLE);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else if (requestCode == imagesecpload) {
                try {
                    imagemapsec = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                    uploadimage2.setImageBitmap(imagemapsec);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else if (requestCode == requestcamera) {


               if (uploadimage1.getId()==R.id.uploadimage1){

                    Bitmap testurl = (Bitmap) data.getExtras().get("data");
                    uploadimage1.setImageBitmap(testurl);
                    uploadimage2.setVisibility(View.VISIBLE);
                }else if (uploadimage2.getId()==R.id.uploadimage2){
                    Bitmap testurltwo = (Bitmap) data.getExtras().get("data");
                    uploadimage2.setImageBitmap(testurltwo);

              }


            }
        }

    }

1 个答案:

答案 0 :(得分:0)

我使用onActivityResult和变量

的增量解决了我的问题