Choose/Take 来自 Android 中 AlertDialog 的照片
Choose/Take a Photo from AlertDialog in Android
在我的应用程序中有一个弹出窗口 activity,用户可以在其中编辑他的信息。
我正在使用 this 教程显示来自 gallery/camera 的照片。我有一堆 xml 个文件,每个警报对话框一个。
在我的 onActivityResult
变量中 editPhoto
无法识别。它在我的 editProfilePhoto
中声明。我尝试了很多方法,将其声明为 final,在 onCreate
中声明,移动 onActivityResult
,等等,但我总是会遇到一些不同的错误。
解决此问题的最佳方法是什么。
我的错误:
Error:(353, 13) error: cannot find symbol variable editPhoto
我的弹出窗口 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);
}
}
}
这是 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 所说的那样在全球范围内声明。
在我的应用程序中有一个弹出窗口 activity,用户可以在其中编辑他的信息。 我正在使用 this 教程显示来自 gallery/camera 的照片。我有一堆 xml 个文件,每个警报对话框一个。
在我的 onActivityResult
变量中 editPhoto
无法识别。它在我的 editProfilePhoto
中声明。我尝试了很多方法,将其声明为 final,在 onCreate
中声明,移动 onActivityResult
,等等,但我总是会遇到一些不同的错误。
解决此问题的最佳方法是什么。
我的错误:
Error:(353, 13) error: cannot find symbol variable editPhoto
我的弹出窗口 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);
}
}
}
这是 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 所说的那样在全球范围内声明。