从图库中获取 3 张图像并将其添加到图像视图

Get 3 Images from gallery and add it to image view

我正在尝试将 3 张图像放入 ImageView。但问题是当我从图库中选择一张图片时,同一张图片显示给所有 imageView,我正在使用 startActivityForResult(intent, 3);

如何 select 为单独的 imageView 分离图像?

这是我选择图片的代码

  editImage1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);
        }
    });

    editImage2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);
        }
    });

    editImage3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);  
        }
    });             // other clicklistener also here

这是 activity 结果的代码

 @SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);



    try {


        if (data == null){

            return;
        }


        if (data.getData() != null) {

            photoUri1 = data.getData();
            photoUri2 = data.getData();
            photoUri3 = data.getData();
            photoUri4 = data.getData();
            photoUri5 = data.getData();
            photoUri6 = data.getData();

            userImage1.setImageURI(photoUri1);
            userImage2.setImageURI(photoUri2);
            userImage3.setImageURI(photoUri3);
            petImage1.setImageURI(photoUri4);
            petImage2.setImageURI(photoUri5);
            petImage3.setImageURI(photoUri6);


        }




        Toast.makeText(getContext(), "Photo selected!", Toast.LENGTH_SHORT).show();



    }catch (Exception e){

        Toast.makeText(getContext(), "Error"+e, Toast.LENGTH_SHORT).show();

    }



}

有没有单独选图的机会

参考:

有多种实现方式。也许您可以对不同的图像使用不同的 requestCode。您的代码:

            photoUri1 = data.getData();
            photoUri2 = data.getData();
            photoUri3 = data.getData();
            photoUri4 = data.getData();
            photoUri5 = data.getData();
            photoUri6 = data.getData();

            userImage1.setImageURI(photoUri1);
            userImage2.setImageURI(photoUri2);
            userImage3.setImageURI(photoUri3);
            petImage1.setImageURI(photoUri4);
            petImage2.setImageURI(photoUri5);
            petImage3.setImageURI(photoUri6);

正在阅读 6 次相同的内容并将其放入 6 张图像中

由于这些原因,OnActivityResult 方法已被弃用。

像这样使用 Activity Results Api

var resultLauncherImage1 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 1
    }
}


var resultLauncherImage2 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 2
    }
}


var resultLauncherImage3 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 3
    }
}



fun openSomeActivityForResult() {
    val intent = Intent(this, SomeActivity::class.java)
    resultLauncherImage1 .launch(intent)
resultLauncherImage2.launch(intent)
resultLauncherImag3.launch(intent)
}