0

i have 4 diffrent imageview and try to click on all to upload diffrent images like LIcence,Rc,Profile etc . I want to uploaded image from gallery or camera dialog.. like this layout. like uper layout example

5 Answers5

1

Please check my updated answer. This is just an example. Hope you understand from this

  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);
            }
        } 
    }
}
Paras Watts
  • 2,565
  • 4
  • 21
  • 46
  • i have 4 imageview and try to click all of imageview to bind diffrent images like licence ,Rc,Profile picture from caemra of gallery – Chandan Baba Jul 06 '17 at 13:01
  • please see mention Layout Image @paras – Chandan Baba Jul 06 '17 at 13:09
  • You can declare different codes for different buttons. Then startActivityForResult and pass the code. Then check the returned code and set the image on different image views. Like I only used one button for the camera and one for the gallery. – Paras Watts Jul 07 '17 at 06:21
  • forget about buttons ,i want to click on each and every imageview and select image from camera or galery and set on imageview which imageview clicked https://stackoverflow.com/users/8091876/paras-watts – Chandan Baba Jul 07 '17 at 06:26
  • Whether click on button or ImageView . You have to declare different image picking codes and then pass those codes in startActivityForResults. For four ImageView use four different codes and get those codes in onActivityResult. Then based on returned codes set the image on ImageView – Paras Watts Jul 07 '17 at 06:30
0

EDIT Since you are using recyclerview or listview kind of view, you can associate tag to each viewholder and then use that tag for resolving imageview. Now you can get unique tag based on position in view. For eg in recyclerview you can use getAdapterPosition().

Associate OnClicks of each imageview with a request code. In onActivityResult resolve them and put images accordingly.

Rushi M Thakker
  • 679
  • 2
  • 15
  • 34
0
 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);
    }  

    }

}
0

I would like to suggest to refer this site, It's implementation is easy and straight forward...

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

Rahul Vansh
  • 171
  • 2
  • 13
0

In addition to the perfectly working answer by @paras, if someone wants to include the Camera option also then please refer to the below steps:

  1. Add a dialog box which pops-up on image click and asks to chose either Camera or Gallery:

    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. Use this method to open the Camera intent:

    private void openCamera() {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, REQUEST_CAMERA);
    }
    
  3. Now in the onActivityResult() method, include this to check whether the intent is from Camera:

    if (requestCode == REQUEST_CAMERA) {
        Bundle bundle = data.getExtras();
        final Bitmap bitmap = (Bitmap) Objects.requireNonNull(bundle).get("data");
        imageView.setImageBitmap(bitmap);
    }
    
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Ankush Chauhan
  • 1,433
  • 2
  • 17
  • 22