Android : 如何将图像从相机或图库传递到另一个片段?

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

我有一个位图作为要设置的通用变量,但是当我 运行 我的应用程序时,它没有显示任何内容。

这是我的代码:

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);
            }
        }
    }
function(element, percentX, percentY){
    var tolerance = 0.01;   //needed because the rects returned by getBoundingClientRect provide the position up to 10 decimals
    if(percentX == null){
        percentX = 100;
    }
    if(percentY == null){
        percentY = 100;
    }

    var elementRect = element.getBoundingClientRect();
    var parentRects = [];

    while(element.parentElement != null){
        parentRects.push(element.parentElement.getBoundingClientRect());
        element = element.parentElement;
    }

    var visibleInAllParents = parentRects.every(function(parentRect){
        var visiblePixelX = Math.min(elementRect.right, parentRect.right) - Math.max(elementRect.left, parentRect.left);
        var visiblePixelY = Math.min(elementRect.bottom, parentRect.bottom) - Math.max(elementRect.top, parentRect.top);
        var visiblePercentageX = visiblePixelX / elementRect.width * 100;
        var visiblePercentageY = visiblePixelY / elementRect.height * 100;
        return visiblePercentageX + tolerance > percentX && visiblePercentageY + tolerance > percentY;
    });
    return visibleInAllParents;
};

您应该传递设置了位图的视图 不是使用新充气机的那个。

不要通过这个

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

改为使用现有视图

builder.setView(view)

最终代码

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();
    }