尽管是高度和宽度的wrap_content,但是在imageview中显示的图像太小了

时间:2014-11-17 08:55:35

标签: java android

我正在开发一个项目,我点击一个按钮打开相机并拍照,然后回到项目,它显示在imageview中。我将图像保存到文件中,当我打开来自我手机目录的图片显示为大尺寸。如何让图片在图片视图中显示得更大?

代码:

btncamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                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
                //image_uri=data.getData().toString();
                previewCapturedImage();
            } else if (resultCode == getActivity().RESULT_CANCELED) {
                // user cancelled Image capture
                image_path=null;
                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();



                options.inSampleSize = 4;   

                final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
                ExifInterface exif = null;
                try {
                    exif = new ExifInterface(fileUri.getPath());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

                int rotationAngle = 0;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

                Matrix matrix = new Matrix();
                matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
                Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);

            iv_preview.setImageBitmap(rotatedBitmap);

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
     * returning image / video
     */
    private  File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
//      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
//              Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {

            //change image_path file name required for marina with the name of the file
            mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg");
//          if(mediaFile.exists())
//          {
//              mediaFile.delete();
//              mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg");
//          }
            //IF YOU MAKE CHANGE TO THE FILE NAME PATH ALSO CHANGE IN loadpreviousimage method
            image_path=mediaStorageDir.getPath() + File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg";
        }  else {
            return null;
        }

        return mediaFile;
    }

是因为这条线options.inSampleSize = 4;,我得到一张小图片吗?

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

是的,inSampleSize如果为> 1则会对您的图像进行子样本处理;用来缩小相当大的图像。 Source here. 而且,您可以使用

ImageView中的

android:scaleType="fitXY"可使您的图片适合ImageView

答案 1 :(得分:0)

使用此方法进行解码,

public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70//put size you want;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

希望这会对你有所帮助