在不同的图像视图上显示不同的文件选择器选择

Displaying different file chooser selections on different Imageviews

我有一个 activity 有 5 个 ImageView。我已经为所有这些视图设置了点击监听器。单击图像视图时,会打开一个文件选择器,一旦选择了图像,它就应该显示在最初单击的图像视图中。我的问题是我不知道如何识别单击了哪个图像视图,从而在正确的视图上显示图像。下面是我的代码,目前只能在 'hard-coded' 视图中显示所选图像。

public class event_creator_view_activity extends AppCompatActivity {
    private ImageView display, img1,img2,img3,img4;
    private Uri imageuri;
    private static final int PICK_IMAGE_REQUEST = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_creator_view_activity);

        display = findViewById(R.id.creator_display_image);
        img1 = findViewById(R.id.creator_img1);
        img2 = findViewById(R.id.creator_img2);
        img3 = findViewById(R.id.creator_img3);
        img4 = findViewById(R.id.creator_img4);

        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser();
            }
        });
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser();
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser();
            }
        });
        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser();
            }
        });
        img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser();
            }
        });

    }


    private void openfileChooser(){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }
    private String getFileExtension(Uri uri){
        ContentResolver cR = this.getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.getExtensionFromMimeType(cR.getType(uri));
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==PICK_IMAGE_REQUEST&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){
            imageuri = data.getData();
            Picasso.get().load(imageuri).into(display);
            //  uploadFile();
        }
    }
}
    public class event_creator_view_activity extends AppCompatActivity {
    private ImageView display, img1,img2,img3,img4;
    private Uri imageuri;
    private static final int PICK_IMAGE_REQUEST_ONE = 1;
    private static final int PICK_IMAGE_REQUEST_TWO = 2;
    private static final int PICK_IMAGE_REQUEST_THREE = 3;
    private static final int PICK_IMAGE_REQUEST_FOUR = 4;
    private static final int PICK_IMAGE_REQUEST_FIVE = 5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_creator_view_activity);

        display = findViewById(R.id.creator_display_image);
        img1 = findViewById(R.id.creator_img1);
        img2 = findViewById(R.id.creator_img2);
        img3 = findViewById(R.id.creator_img3);
        img4 = findViewById(R.id.creator_img4);

        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser(PICK_IMAGE_REQUEST_ONE);
            }
        });
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser(PICK_IMAGE_REQUEST_TWO);
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser(PICK_IMAGE_REQUEST_THREE);
            }
        });
        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser(PICK_IMAGE_REQUEST_FOUR);
            }
        });
        img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openfileChooser(PICK_IMAGE_REQUEST_FIVE);
            }
        });

    }


    private void openfileChooser(int requestcode){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, requestcode);
    }
    private String getFileExtension(Uri uri){
        ContentResolver cR = this.getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.getExtensionFromMimeType(cR.getType(uri));
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==PICK_IMAGE_REQUEST_ONE&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){
            imageuri = data.getData();
            Picasso.get().load(imageuri).into(display);
            //  uploadFile();
        }else if(requestCode==PICK_IMAGE_REQUEST_TWO&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){

            imageuri = data.getData();
            Picasso.get().load(imageuri).into(img1);

        }else if(requestCode==PICK_IMAGE_REQUEST_THREE&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){

            imageuri = data.getData();
            Picasso.get().load(imageuri).into(img2);

        }else if(requestCode==PICK_IMAGE_REQUEST_FOUR&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){

            imageuri = data.getData();
            Picasso.get().load(imageuri).into(img3);

        }else if(requestCode==PICK_IMAGE_REQUEST_FIVE&&resultCode==this.RESULT_OK&&data!=null&&
                data.getData()!=null){

            imageuri = data.getData();
            Picasso.get().load(imageuri).into(img4);
        }
    }
  }

希望对您有所帮助...