3

in my android app , i want to upload image to the server. the problem that the server will not accept images with size larger than 2M. but the user can choose an image larger than 2M.

so I want to build a code that will make the image smaller than 2M.

I have two approaches :

  1. to resize the image dimensions. as below :

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    
  2. also I can compress the image

    image.compress(Bitmap.CompressFormat.PNG, 10, fos);
    

what the difference between these two approaches ?

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
david
  • 3,310
  • 7
  • 36
  • 59

1 Answers1

8

Image re sizing means you're going to shorten the resolution of the image. suppose user selects a 1000*1000 px image. you're going to convert the image into a 300*300 image. thus image size will be reduced.

And Image compression is lowering the file size of the image without compromising the resolution. Of course lowering file size will affect the quality of the image. There are many compression algorithm available which can reduce the file size without much affecting the image quality.

Faysal Ahmed
  • 1,592
  • 13
  • 25
  • thank you for your answer. could you please write a code that will compress the image to 2M. thanks. – david Jul 01 '15 at 08:56
  • thank you for your comment. but that doesn't compress to specific byte count. instead it shows that the compress doesn't change image height and width ! – david Jul 01 '15 at 15:48