1

I use this function to reduce the size of image before uploading it,But using below method my file size is increasing

Before use below code my file size---> 157684

after using this code my file size ----->177435

Can some one help me please how can i reduce file size before upload to server

code:

public File saveBitmapToFile(File file){
    try {

        // BitmapFactory options to downsize the image
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;
        // factor of downsizing the image

        FileInputStream inputStream = new FileInputStream(file);
        //Bitmap selectedBitmap = null;
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        // The new size we want to scale to
        final int REQUIRED_SIZE=75;

        // 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;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = new FileInputStream(file);

        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();

        // here i override the original image file
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);

        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);

        return file;
    } catch (Exception e) {
        return null;
    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Krish
  • 4,166
  • 11
  • 58
  • 110

5 Answers5

1

Change this line:

    selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);

to

int mCompressedSize = 50; // 0 is lowest and 100 original
selectedBitmap.compress(Bitmap.CompressFormat.PNG, mCompressedSize, outputStream);

Hope this will help.

Chetan Pushpad
  • 345
  • 1
  • 9
1

This what I use to reduce my image size without compressing :

public static Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}

You just need to enter correct new height and width to fit your needs

Jackyto
  • 1,569
  • 3
  • 15
  • 33
1

We want to make thumbnail of an image, so we need to first take the ByteArrayOutputStream and then pass it into Bitmap.compress() method.

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
youBitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

more about the function, from the docs

Rahul Ahuja
  • 812
  • 10
  • 24
1

If your output file is larger:

  • it can mean that scale is wrong. And you save the file with 100% quality so it can grow

  • compression on the input file is extremely heavy and even though you scale it, using no compression on the output still generates a larger file

LukeJanyga
  • 1,115
  • 9
  • 16
0

Try

int compressionRatio = 2; //1 == originalImage, 2 = 50% compression, 4=25% compress
File file = new File (imageUrl);
try {
    Bitmap bitmap = BitmapFactory.decodeFile (file.getPath ());
    bitmap.compress (Bitmap.CompressFormat.JPEG, compressionRatio, new FileOutputStream (file));
}
catch (Throwable t) {
    Log.e("ERROR", "Error compressing file." + t.toString ());
    t.printStackTrace ();
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53