Search code examples
androidandroid-cameraandroid-imageviewandroid-image

how to capture image from camera, in fragment,


I am new to Android, and I have done lots of training but the image does not load from the camera. Below is my code for capturing image from camera or gallery:

    public void showDiloag(){
    Dialog dialog = new Dialog(getActivity());
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Choose Image Source");
    builder.setItems(new CharSequence[] { "Gallery", "Camera" },
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog,
                        int which) {
                    switch (which) {
                    case 0:
                        Intent intent = new Intent(
                                Intent.ACTION_GET_CONTENT);
                        intent.setType("image/*");

                        Intent chooser = Intent
                                .createChooser(
                                        intent,
                                        "Choose a Picture");
                        getAcitivity.startActivityForResult(
                                chooser,
                                ACTION_REQUEST_GALLERY);

                        break;

                    case 1:
                        Intent cameraIntent = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(
                                cameraIntent,
                                ACTION_REQUEST_CAMERA);

                        break;

                    default:
                        break;
                    }
                }
            });

    builder.show();
    dialog.dismiss();
}

And for display that photo:

      @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("OnActivityResult");
    if (resultCode == getActivity().RESULT_OK) {
        if (requestCode == Utils.ACTION_REQUEST_GALLERY) {
            // System.out.println("select file from gallery ");
            Uri selectedImageUri = data.getData();
            String tempPath = JuiceAppUtility.getPath(
                    selectedImageUri, getActivity());

            Bitmap bm = JuiceAppUtility
                    .decodeFileFromPath(tempPath);
            imgJuice.setImageBitmap(bm);
        } else if (requestCode == Utils.ACTION_REQUEST_CAMERA) {
            Bitmap photo = (Bitmap) data.getExtras()
                    .get("data");
            imgJuice.setImageBitmap(photo);
        }
    }
}

Also image is captured from camera and choose from gallery but it will not load in ImageView. Can anybody help me please?


Solution

  • Ya i found your problem

    just remove below line and

               getAcitivity.startActivityForResult(
                                chooser,
                                ACTION_REQUEST_GALLERY);
    

    and write down below code

                startActivityForResult(
                                chooser,
                                ACTION_REQUEST_GALLERY);
    

    just remove getActivity