Search code examples
androidandroid-cameraandroid-imageviewandroid-galleryandroid-dialog

Android: clicking the image in camera and show the image in dialogbox


i m using a custom dialog with an ImageView and 2 Button.

  1. first button to pick image from gallery
  2. second button to click image from camera

i want to show the image in the ImageView of the dialog..

btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Dialog();   
        }

        private void Dialog() {
            // TODO Auto-generated method stub
            final Dialog dialog=new Dialog(editevent1.this);
            dialog.setContentView(R.layout.promote2);
            ImageView image =(ImageView)findViewById(R.id.image_camera);
            Button d=(Button)dialog.findViewById(R.id.button2);
            Button f=(Button)dialog.findViewById(R.id.button3);

            dialog.show();
            d.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(intent, 1);


                }
            });
            f.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent cameraintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    startActivityForResult(cameraintent, CAMERA_PIC_REQUEST);
                }
            });


            }


});



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if( requestCode == CAMERA_PIC_REQUEST)
    {   
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView)findViewById(R.id.image_camera);
        image.setImageBitmap(thumbnail);
    }
    else 
    {
        Toast.makeText(this, "Picture NOt taken", Toast.LENGTH_LONG);            
    }

}

please tell me how do i get the image in ImageView which is in DialogBox for both when picked from gallery and when clicked from camera. thanks


Solution

  • I am not sure this can be done using a Dialog but instead of a custom dialog, try and use an Activity that has the UI of a dialog. To use an Activity as a dialog you need to define this in the AndroidManifest.xml:

    <activity android:theme="@android:style/Theme.Dialog" />

    EDIT for Comment

    1. If you used my suggestion (an Activity with the UI of a Dialog let's name it DialogActivity for our purposes), you do not have to do that since you can override the onActivityResult method in your DialogActivity.
    2. If you have not used my suggestion or have used it but need to pass the taken image to another Activity either way. To pass an image that was taken by the camera app from one Activity to another, i believe the most efficient way is to pass in the Intent as a String the path of that image and access the path on the second Activity. Something like this:

    In the first Activity (the caller Activity), let's call it A:

    Intent i = new Intent(A.this, B.class);
    i.putExtra("path", path); //The path is a String variable holding the path where the image was saved.
    startActivity(i);
    

    In the second Activity (the Activity being called), let's call it B:

    String image_path = getIntent().getExtras().getString("path");
    Bitmap bitmap = BitmapFactory.decodeFile(image_path);