将从相机接收的图像设置为不适用于较新的Android版设备的imageview

时间:2014-11-10 05:49:07

标签: java android

我正在开发一个项目,用户可以将其定向到默认摄像头拍照并在imageview中进行设置。代码如下:

点击按钮:

btncamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                captureImage();
            }
        });

captureimage方法:

private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode ==getActivity().RESULT_OK) {
                // successfully captured the image
                // display it in image view
                previewCapturedImage();
            } else if (resultCode == getActivity().RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getActivity(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getActivity(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        } 
    }
    private void previewCapturedImage() {
        try {
            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 1;

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);

            iv_preview.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

问题是在imageview中设置的imagepreview适用于android 2.6设备,但不适用于较新的android kitkat版本。

我已将imageview设置为wrap_content的宽度和高度。

我该如何解决这个问题?

0 个答案:

没有答案