Search code examples
androidfragmentandroid-camera2

Display bitmap image in an alert dialog in fragment


I add a new image to Camera2Fragement class to display bitmap image in an alert dialog but I only get empty dialog without any image. What am i doing wrong. Is it related to fragment where we can not upload the image to image view or another problem. Here is my code:

public void onClick(View view) {
        switch (view.getId()) {
            case R.id.picture: {
                takePicture();
                break;
            }
            case R.id.info: {
                Activity activity = getActivity();
                if (null != activity) {
                    new AlertDialog.Builder(activity)
                            .setMessage(R.string.intro_message)
                            .setPositiveButton(android.R.string.ok, null)
                            .show();
                }
                break;
            }
            case R.id.result: { // added code
                Activity activity = getActivity();
                if (null != activity) {

                    view = View.inflate(activity, R.layout.dialog_layout, null);

                    ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);

                    Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);
                    //imgRefInflated.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/DCIM/mResultImg.jpg"));
                    Log.d(TAG, "I can reach here");


                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

                    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

                    AlertDialog dialog = builder.create();
                    dialog.setView(view);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.show();

                }
                break;
            }
        }
    }

Solution

  • You are trying to set image to ImageView inside of AlertDialog, which is not shown. Rearange your code like this:

    view = View.inflate(activity, R.layout.dialog_layout, activity);
    
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setView(view)
            .create();
    dialog.show();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
    ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);
    Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);