title: Choose/Take a Photo from AlertDialog in Android
body: <p>In my app there is a popup activity where the user can edit his info. 
  I am using <a href="http://www.devexchanges.info/2016/05/android-basic-training-course-working.html" rel="nofollow noreferrer">this</a> tutorial to display a photo from gallery/camera. I have a bunch of xml files, one for every alert dialog.</p>
  
  <p>In my <code>onActivityResult</code> variable <code>editPhoto</code> is not recognized. It is declared in my <code>editProfilePhoto</code>. I have tried a bunch of things, declaring it final, declaring it in <code>onCreate</code>, moving <code>onActivityResult</code>, etc, but I always get some different error.</p>
  
  <p>What would be the best way to fix this.</p>
  
  <p>My error:</p>
  
  <pre><code>Error:(353, 13) error: cannot find symbol variable editPhoto
  </code></pre>
  
  <p>My popup activity PopupEditProfile:</p>
  
  <pre><code>public class PopupEditProfile extends Activity {
  
      private RelativeLayout editNameButton, editEmailButton, editAboutButton, changePasswordButton, editPhoneNumber, editProfilePhoto;
      private Button saveButton, cancelButton;
      private ImageView uploadImageButton, takePhotoButton, imagePlaceholder;
      private PopupWindow editNamePopup, editEmailPopup, editAboutPopup, changePasswordPopup, editPhonePopup, uploadImagePopup;
      private LayoutInflater layoutInflater;
      private LinearLayout linearLayout;
      private SQLiteHandler db; 
      private static final int CAMERA_PICK = 1;
      private static final int PICK_FROM_GALLERY = 2;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.popup_edit_profile);
  
          db = new SQLiteHandler(getApplicationContext());
          HashMap&lt;String, String&gt; user = db.getUserDetails();
          final String name = user.get("name");
          String email = user.get("email");
          final String id = user.get("uid");
          DisplayMetrics displayMetrics = new DisplayMetrics();            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
          getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          linearLayout = (LinearLayout) findViewById(R.id.popup_edit_profile);
  
          // Edite profile dialogs
          editNameButton = (RelativeLayout) findViewById(R.id.rlEditMyName);
          editNameButton.setOnClickListener(new View.OnClickListener() {
              ...            
          });
          editEmailButton = (RelativeLayout) findViewById(R.id.rlEditEmail);
          editEmailButton.setOnClickListener(new View.OnClickListener() {
              ...
          });
          editAboutButton = (RelativeLayout) findViewById(R.id.rlEditAbout);
          editAboutButton.setOnClickListener(new View.OnClickListener() {
              ...
          });
          changePasswordButton = (RelativeLayout) findViewById(R.id.rlEditPassword);
          changePasswordButton.setOnClickListener(new View.OnClickListener() {
              ...
          });
          editPhoneNumber = (RelativeLayout) findViewById(R.id.rlEditPhone);
          editPhoneNumber.setOnClickListener(new View.OnClickListener() {
              ... 
          });
  
          // EDIT PHOTO DIALOG
          editProfilePhoto = (RelativeLayout) findViewById(R.id.rlEditPhoto);
          editProfilePhoto.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  AlertDialog.Builder photoBuilder = new AlertDialog.Builder(PopupEditProfile.this);
                  View photoView = getLayoutInflater().inflate(R.layout.popup_edit_photo, null);
                  final ImageView editPhoto = (ImageView) photoView.findViewById(R.id.imagePlaceholder);
                  final ImageView cameraImageView = (ImageView) photoView.findViewById(R.id.cameraImageView);
                  final ImageView galleryImageView = (ImageView) photoView.findViewById(R.id.galleryImageView);
                  Button saveButtonPhoto = (Button) photoView.findViewById(R.id.saveButtonPhoto);
                  Button cancelButtonPhoto = (Button) photoView.findViewById(R.id.cancelButtonPhoto);
  
                  ColorGenerator generator = ColorGenerator.MATERIAL;
                  int color = generator.getColor(id);
                  String firstLetter = name.substring(0, 1);
                  TextDrawable textDrawable = TextDrawable.builder().buildRect(firstLetter, color);
                  editPhoto.setImageDrawable(textDrawable);
  
                  cameraImageView.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                          startActivityForResult(cameraIntent, CAMERA_PICK);
                      }
                  });
  
                  galleryImageView.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Intent intent = new Intent();
                          intent.setType("image/*"); //set type for files (image type)
                          intent.setAction(Intent.ACTION_GET_CONTENT);
                          startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_FROM_GALLERY);
                      }
                  });
  
                  photoBuilder.setView(photoView);
                  final AlertDialog photoDialog = photoBuilder.create();
                  photoDialog.show();
                  saveButtonPhoto.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          Toast.makeText(PopupEditProfile.this,
                                  R.string.success,
                                  Toast.LENGTH_SHORT).show();
                          photoDialog.dismiss();
                      }
                  });
                  cancelButtonPhoto.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                      photoDialog.dismiss();
                      }
                  });
              }
          });
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == CAMERA_PICK &amp;&amp; resultCode == RESULT_OK) {
              Bitmap photo = (Bitmap) data.getExtras().get("data");
              //set photo bitmap to ImageView
              editPhoto.setImageBitmap(photo);
          } else if (requestCode == PICK_FROM_GALLERY &amp;&amp; resultCode == RESULT_OK) {
              Uri selectedImage = data.getData();
              editPhoto.setImageURI(selectedImage);
          }
      }
  }
  </code></pre>
  
  <p>And this is the xml layout popup_edit_photo:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
  &lt;RelativeLayout
      android:id="@+id/layout_imageviews"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;ImageView
          android:id="@+id/imagePlaceholder"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:scaleType="fitCenter"
          android:adjustViewBounds="true"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:contentDescription="@string/profile_photo"
          android:gravity="center"
          android:src="@drawable/default_profile"/&gt;
  
      &lt;ImageView
          android:id="@+id/cameraImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:padding="20dp"
          android:src="@android:drawable/ic_menu_camera"
          android:contentDescription="@string/go_to_camera_imageview" /&gt;
  
      &lt;ImageView
          android:id="@+id/galleryImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentEnd="true"
          android:layout_alignParentTop="true"
          android:contentDescription="@string/go_to_gallery_imageview"
          android:padding="20dp"
          android:src="@android:drawable/ic_menu_gallery" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;LinearLayout
          android:contentDescription="@string/save_cancel_buttons"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="bottom"
          android:orientation="horizontal"
          android:weightSum="1"
          tools:ignore="UselessParent"&gt;
  
          &lt;Button
              android:id="@+id/cancelButtonPhoto"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".5"
              android:background="@drawable/button_clicker"
              android:text="@string/cancel"
              android:textColor="@color/white"
              tools:ignore="ButtonStyle" /&gt;
  
          &lt;Button
              android:id="@+id/saveButtonPhoto"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".5"
              android:background="@drawable/button_clicker"
              android:text="@string/save"
              android:textColor="@color/white"
              tools:ignore="ButtonStyle" /&gt;
      &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>