簡體   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