如何在 android 中从图库和相机中的特定 imageView 点击上传特定图像

How to upload particular image on particular imageView click from gallery and camera in android

我有 4 个不同的图像视图,并尝试单击所有以上传不同的图像,如 LIcence、Rc、Profile 等。我想从画廊或相机对话框上传图像.. 喜欢 this 布局。 像上面的布局示例

编辑 由于您使用的是 recyclerview 或 listview 类型的视图,因此您可以将标签关联到每个 viewholder,然后使用该标签来解析 imageview。现在您可以根据视图中的位置获得唯一标签。例如,在 recyclerview 中,您可以使用 getAdapterPosition()。

将每个图像视图的 OnClicks 与请求代码相关联。在 onActivityResult 中解决它们并相应地放置图像。

 private void imageBrowse() {
if(camera){
 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
}else{
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}
}

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {

        if(requestCode == PICK_IMAGE_REQUEST){
            Uri picUri = data.getData();

            filePath = getPath(picUri);
            uri =picUri;
            Log.d("picUri", picUri.toString());
            Log.d("filePath", filePath);

            imageView.setImageURI(picUri);

        }

  if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  

    }

}

请检查我更新的答案。这只是一个例子。希望你从中明白

  ImageView profile_pic;
    private static final int SELECT_PICTURE1 = 100;
    private static final int SELECT_PICTURE2 = 101;
    private static final int SELECT_PICTURE3 = 102;
    private static final int SELECT_PICTURE4 = 103;





picture1 = (ImageView) view.findViewById(R.id.picture1);
picture2 = (ImageView) view.findViewById(R.id.picture2);
picture3 = (ImageView) view.findViewById(R.id.picture3);
picture4 = (ImageView) view.findViewById(R.id.picture4);
picture1.setOnClickListener(this);
picture2.setOnClickListener(this);
picture3.setOnClickListener(this);
picture4.setOnClickListener(this);







@Override
    public void onClick(View v) {
        if (v.getId() == R.id.picture1) {
             Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE1);
        }

         if (v.getId() == R.id.picture2) {
             Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE2);
        }
         if (v.getId() == R.id.picture3) {
             Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE3);
        }

         if (v.getId() == R.id.picture4) {
             Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE4);
        }


    }




@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE1) {
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                String path = selectedImageUri.getPath();
                Log.e("image path", path + "");
                pricture1.setImageURI(selectedImageUri);
            }
        } 

        if (requestCode == SELECT_PICTURE2) {
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                String path = selectedImageUri.getPath();
                Log.e("image path", path + "");
                picture2.setImageURI(selectedImageUri);
            }
        } 

        if (requestCode == SELECT_PICTURE3) {
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                String path = selectedImageUri.getPath();
                Log.e("image path", path + "");
                picture3.setImageURI(selectedImageUri);
            }
        } 

        if (requestCode == SELECT_PICTURE4) {
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                String path = selectedImageUri.getPath();
                Log.e("image path", path + "");
                picture4.setImageURI(selectedImageUri);
            }
        } 
    }
}

我想建议参考这个网站,它的实施简单直接...

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

除了@paras 的完美答案,如果有人还想包含相机选项,请参考以下步骤:

  1. 添加点击图片弹出的对话框,要求选择相机或图库:

    final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
    
    AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Camera")) {
                openCamera();
            } else if (items[item].equals("Gallery")) {
                openGallery();
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
    
  2. 使用此方法打开Camera intent:

    private void openCamera() {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, REQUEST_CAMERA);
    }
    
  3. 现在在onActivityResult()方法中,包括这个来检查意图是否来自相机:

    if (requestCode == REQUEST_CAMERA) {
        Bundle bundle = data.getExtras();
        final Bitmap bitmap = (Bitmap) Objects.requireNonNull(bundle).get("data");
        imageView.setImageBitmap(bitmap);
    }