在相机意图中选择图像尺寸

时间:2018-11-03 04:08:50

标签: android android-studio android-camera-intent

我是这个论坛的新手,所以请多多包涵,并轻轻指出任何错误, 所以我正在一个项目中,我将图像上传到服务器,现在我想限制图像的大小,我给“ Click image”选项,我的代码将打开默认的相机意图并单击图片。 ,或“从图库中选择”。 我的问题是关于“点击图片”,现在用户点击图片时,我可以预设可以点击的图片最大尺寸吗?

2 个答案:

答案 0 :(得分:0)

您可以简单地获取文件的大小。当您购买图像时,您需要存储图像。之后,您可以使用以下代码段获取大小

String imagePath = Environment.getExternalStorageDirectory() + "/yourImagefile.png";
File imageFile = new File(imagePath );
long filelength = imageFile .length();
length = filelength/1024;

length为您提供了KB大小。那么您可以添加下面的条件

 if(length>sizeyouwant){
    //delete image and toast message with info
    if(imageFile.exists()) {
       imageFile.delete();
    }
    Toast.makeText(getApplicationContext(),
    "Image is not saved due to image size exceeds limit....",
    Toast.LENGTH_SHORT).show();
  }

答案 1 :(得分:0)

您可以使用onActivityResult方法调整图片大小,请尝试以下代码段

 public static Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage)
            throws IOException {
        int MAX_HEIGHT = 1024;
        int MAX_WIDTH = 1024;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
        BitmapFactory.decodeStream(imageStream, null, options);
        imageStream.close();
        options.inSampleSize = calculateInSampleSizes(options, MAX_WIDTH, MAX_HEIGHT);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        imageStream = context.getContentResolver().openInputStream(selectedImage);
        Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
        img = rotateImageIfRequired(context, img, selectedImage);
        return img;
    }

    private static int calculateInSampleSizes(BitmapFactory.Options options,
                                             int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee a final image
            // with both dimensions larger than or equal to the requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

            // This offers some additional logic in case the image has a strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might still
            // end up being too large to fit comfortably in memory, so we should
            // be more aggressive with sample down the image (=larger inSampleSize).

            final float totalPixels = width * height;

            // Anything more than 2x the requested pixels we'll sample down further
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
    private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {

        InputStream input = context.getContentResolver().openInputStream(selectedImage);
        ExifInterface ei;
        if (Build.VERSION.SDK_INT > 23)
            ei = new ExifInterface(input);
        else
            ei = new ExifInterface(selectedImage.getPath());

        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return rotateImage(img, 90);
            case ExifInterface.ORIENTATION_ROTATE_180:
                return rotateImage(img, 180);
            case ExifInterface.ORIENTATION_ROTATE_270:
                return rotateImage(img, 270);
            default:
                return img;
        }
    }
    private static Bitmap rotateImage(Bitmap img, int degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
        img.recycle();
        return rotatedImg;
    }

您只需要调用handleSamplingAndRotationBitmap方法,您将获得一个可以自行设置大小的位图。 PS:如果通过sumsung手机的旋转捕获的某些图片不正确,那么我们也需要处理图片的方向,希望可以为您提供帮助。