Search code examples
androidandroid-fragmentsandroid-imageviewandroid-dialogfragmentandroid-bitmap

Android : How to pass an image from camera or gallery to another fragment?


I have a Bitmap as a general variable to set, but when I run my App, it doesn't show anything.

Here my Code:

public AlertDialog FragmentDialog(){
            AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

            LayoutInflater inflater=getActivity().getLayoutInflater();
            View view=inflater.inflate(R.layout.imagenfoto,null);
            dg_image=view.findViewById(R.id.dlg_image);
            dg_image.setImageBitmap(bitmap);

            builder.setView(inflater.inflate(R.layout.imagenfoto,null))
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //dg_image.setImageBitmap(bitmap);
                            dialogInterface.cancel();
                        }
                    });
            return builder.create();
        }
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode== getActivity().RESULT_OK && requestCode==10){
            Uri path=data.getData();
            foto.setImageURI(path);
        }
        */

        if(requestCode==CAMARA_REQUEST && resultCode==Activity.RESULT_OK){
            Bitmap photo=(Bitmap)data.getExtras().get("data");
            bitmap=(Bitmap)data.getExtras().get("data");
            foto.setImageBitmap(photo);
            Log.d("Debug",bitmap.toString());
            //comunicador.setFoto(bitmap);

            /*try {
                setFoto(photo);
            }catch (Exception e){
                Log.d("setGaleria:",e.getMessage());
            }*/
            //cd_imagen.setImageBitmap(photo);
        }
        //***//
        Uri selectedImageUri=null;
        Uri selectedImage;
        if(requestCode==SELECT_FILE && resultCode==Activity.RESULT_OK){
            selectedImage=data.getData();
            String selectedPath=selectedImage.getPath();
            if(selectedPath != null){
                InputStream imageStream=null;

                try {
                    imageStream=getActivity().getContentResolver().openInputStream(
                            selectedImage);
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }

                //Transformamos la URI de la imagen a ImputStream y este a un Bitmap
                Bitmap bmp=BitmapFactory.decodeStream(imageStream);
                bitmap=BitmapFactory.decodeStream(imageStream);
                Log.d("Debug",bitmap.toString());
                /*try {
                    setFoto(bmp);
                }catch (Exception e){
                    Log.d("setGaleria:",e.getMessage());
                }*/

                //Ponemos nuestro Bitmap en un ImageView que tengamos en la vista
                foto.setImageBitmap(bmp);
                //comunicador.setFoto(bmp);

                //cd_imagen.setImageBitmap(bmp);
            }
        }
    }

Solution

  • you should pass the view on which you have set the bitmap not the one using new inflator.

    don't pass this

    builder.setView(inflater.inflate(R.layout.imagenfoto,null))
    

    instead, use the existing view

    builder.setView(view)
    

    Final code

    public AlertDialog FragmentDialog(){
            AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
    
            LayoutInflater inflater=getActivity().getLayoutInflater();
            View view=inflater.inflate(R.layout.imagenfoto,null);
            dg_image=view.findViewById(R.id.dlg_image);
            dg_image.setImageBitmap(bitmap);
    
            builder.setView(view))
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //dg_image.setImageBitmap(bitmap);
                            dialogInterface.cancel();
                        }
                    });
            return builder.create();
        }