简体   繁体   中英

Choose/Take a Photo from AlertDialog in Android

In my app there is a popup activity where the user can edit his info. I am using this tutorial to display a photo from gallery/camera. I have a bunch of xml files, one for every alert dialog.

In my onActivityResult variable editPhoto is not recognized. It is declared in my editProfilePhoto . I have tried a bunch of things, declaring it final, declaring it in onCreate , moving onActivityResult , etc, but I always get some different error.

What would be the best way to fix this.

My error:

Error:(353, 13) error: cannot find symbol variable editPhoto

My popup activity PopupEditProfile:

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<String, String> 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 && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            //set photo bitmap to ImageView
            editPhoto.setImageBitmap(photo);
        } else if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            editPhoto.setImageURI(selectedImage);
        }
    }
}

And this is the xml layout popup_edit_photo:

<?xml version="1.0" encoding="utf-8"?>
<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">

<RelativeLayout
    android:id="@+id/layout_imageviews"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <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"/>

    <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" />

    <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" />

    </RelativeLayout>

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <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">

        <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" />

        <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" />
    </LinearLayout>
    </RelativeLayout>
</LinearLayout>

declare editphoto globally

 ImageView editPhoto;

right now editPhoto object scope is only in clicklistner thats why you cannot use in onActivityResult method.

You have editPhoto created and declared inside onClick method. It should be declared globally as Hitesh Gehlot says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM