简体   繁体   English

Android - 从图库/相机获取图像时,Dialog不会刷新ImageView

[英]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.

相关问题 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸 - Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android Android ImageView无法从图库或相机中获取图像 - Android ImageView can't get image from gallery or camera Android-图像未显示在ImageView中并且无法从相机保存到图库 - Android - Image is not showing in ImageView and not save to gallery from camera 从图库和相机中获取图像(Android Studio) - Getting an image from gallery and camera (Android Studio) 从图库或相机设置图像以适合Imageview android - set image from Gallery or Camera to fit to Imageview android 使用从相机或图库中选择的图像设置ImageView - Set ImageView with selected Image from Camera or Gallery 来自相机或图库的图像视图中的图像加载 - image load in imageview from camera or gallery 从图库或相机中选择图像的对话框 - Dialog to pick image from gallery or from camera 从相机获取图像或从图库上传并在 ImageView (Android Studio) 中显示后,保存图像为 SharedPreferences - Save image is SharedPreferences after Image is get from camera or upload from gallery and display at ImageView (Android Studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM