[英]Android - Dialog not refresing ImageView when getting image from gallery/ camera
So i´m working on an app in which I need to show a dialog, and inside this dialog, the user can press a button to select an image from the gallery or take a photo from the camera and display that image on an ImageView inside a dialog´s ImageView. 所以我正在开发一个应用程序,我需要在其中显示一个对话框,在此对话框中,用户可以按一个按钮从库中选择一个图像或从相机中拍摄一张照片并在ImageView内显示该图像一个对话框的ImageView。 I´m able to take the photo from the camera/gallery, and then i use it as the ImageView image, but it doesn´t show it until i press the button again, as if the layout had to be refreshed.
我可以从相机/图库中拍摄照片,然后我将其用作ImageView图像,但直到我再次按下按钮才会显示它,就像布局必须刷新一样。 This is my code:
这是我的代码:
To call the dialog (get_permissions(); is used on Android M for getting the write and read storage permissions, this works correctly) 要调用对话框(get_permissions();在Android M上用于获取写入和读取存储权限,这可以正常工作)
protected void createDialog() {
final Dialog dialog = new Dialog(WeightActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_add_weight);
Button okButton = (Button) dialog.findViewById(R.id.okButton);
final ImageButton selectImageButton = (ImageButton) dialog.findViewById(R.id.selectImageButton);
final ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
selectImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
get_permissions();
selectImage();
imageView.setImageBitmap(bm);
}
});
dialog.show();
}
private void selectImage() {
final String[] items = WeightActivity.this.getResources().getStringArray(R.array.photo_options);
AlertDialog.Builder builder = new AlertDialog.Builder(WeightActivity.this);
builder.setTitle(R.string.weight_dialog_add_photo);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals(items[0])) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals(items[1])) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
bm = (Bitmap) data.getExtras().get("data");
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, projection, null, null,
null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
}
}
}
Could somebody help me please?? 有人可以帮我吗? Thanks a lot!
非常感谢!
The problem is this, 问题是这个,
get_permissions();
selectImage();
imageView.setImageBitmap(bm);
selectImage()
starts the activity, but it doesn't block; selectImage()
启动活动,但它不会阻塞; it returns immediately. 它会立即返回。 You then call
setImageBitmap()
, probably on a null valued bm
instance field. 然后,您可能在null值
bm
实例字段上调用setImageBitmap()
。
Move, or otherwise trigger the imageView.setImageBitmap()
call in onActivityResult()
, only after you have decoded the image. 仅在解码图像后移动或以其他方式触发
onActivityResult()
中的onActivityResult()
imageView.setImageBitmap()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.