繁体   English   中英

从Android中的AlertDialog中选择/拍照

[英]Choose/Take a Photo from AlertDialog in Android

在我的应用程序中,有一个弹出活动,用户可以在其中编辑他的信息。 我正在使用教程来显示画廊/相机中的照片。 我有一堆xml文件,每个警报对话框一个。

在我的onActivityResult变量中, editPhoto无法识别。 它在我的editProfilePhoto声明。 我尝试了很多事情,最后声明它,在onCreate声明它,移动onActivityResult等,但是我总是遇到一些不同的错误。

解决此问题的最佳方法是什么。

我的错误:

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

我的弹出活动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);
        }
    }
}

这是xml布局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>

全局声明editphoto

 ImageView editPhoto;

现在,editPhoto对象作用域仅在clicklistner中,这就是为什么不能在onActivityResult方法中使用的原因。

您已经在onClick方法中创建并声明了editPhoto 如Hitesh Gehlot所说,应该在全球范围内进行声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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