如何从相机中捕捉图像,碎片,

时间:2013-12-17 14:52:45

标签: android android-camera android-imageview android-image

我是Android新手,我已经完成了很多培训,但图像没有从相机加载。以下是我从相机或图库中捕获图像的代码:

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

显示照片:

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

此外,图像也会从相机中捕获并从图库中选择,但不会在ImageView中加载。请有人帮帮我吗?

2 个答案:

答案 0 :(得分:5)

我找到了你的问题

只需删除以下行

即可
           getAcitivity.startActivityForResult(
                            chooser,
                            ACTION_REQUEST_GALLERY);

并记下下面的代码

            startActivityForResult(
                            chooser,
                            ACTION_REQUEST_GALLERY);

删除getActivity

答案 1 :(得分:0)

片段侧

btnimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPictureDialog();
            }
        });

private void showPictureDialog(){android.app.AlertDialog.Builder pictureDialog = new android.app.AlertDialog.Builder(getActivity());
        pictureDialog.setTitle("Select Action");
        String[] pictureDialogItems = {
                "Select photo from gallery",
                "Capture photo from camera"};
        pictureDialog.setItems(pictureDialogItems,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
//                    choosePhotoFromGallary();
                                choosePhotoFromGallary();
                                break;
                            case 1:
//                    takePhotoFromCamera();
                                takePhotoFromCamera();
                                break;
                        }
                    }
                });

        pictureDialog.show();
    }

private void takePhotoFromCamera() {
        Uri resultUri;
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        resultUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new ContentValues());
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, resultUri);
        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivityForResult(intent, 22);
        }
    }
    public void choosePhotoFromGallary() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(galleryIntent, 11);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK)
        {
            if (requestCode == 11) {
                if (data != null) {
                    Uri contentURI = data.getData();
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentURI);
                        //                    String path = saveImage(bitmap);
                        //                    Toast.makeText(getActivity(), "Image Saved!", Toast.LENGTH_SHORT).show();
                        iv_froncnic.setImageBitmap(bitmap);

                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), "Failed!", Toast.LENGTH_SHORT).show();
                    }
                }
                {
                    Toast.makeText(getActivity(), "Data not found", Toast.LENGTH_SHORT).show();
                }

            }
    }

    }

在父活动中


  @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode==this.RESULT_OK){
        super.onActivityResult(requestCode, resultCode, data);
        setResult(RESULT_OK, data);
        }

    }