-4

I have tried by setting BitmapFactory.Options inSampleSize but its not reducing image below 500 KB.

Image might be any size and must be compressed because i'm storing it on database, Image below 500 KB is my primary requirement. Below is my code:

                        int inSampleSize = 1;

                    Bitmap bitmapImage;//= BitmapFactory.decodeFile(imageUri, options);
                    do {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inSampleSize = inSampleSize;
                        options.inScaled = true;
                        bitmapImage = BitmapFactory.decodeFile(imageUri, options);
                        inSampleSize++;
                    } while (bitmapImage != null && bitmapImage.getByteCount() > 500000);

Please help me on this.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Shailendra Patil
  • 377
  • 2
  • 19
  • 3
    1) Please provide some code you have tried. 2) Please do some research 3) You can never guarantee that you will compress it that much – apmartin1991 Dec 09 '15 at 10:45
  • saving an image on database isn't the best solution. You can save the image on internal storage and save to the database the path of the image as a string. – Vasileios Pallas Dec 09 '15 at 10:50
  • yes, that's a good option. but i need to send the image bytes to the server, and server accepts images which are less than 500 KB in size. – Shailendra Patil Dec 09 '15 at 10:54

1 Answers1

1

Try this

 public Bitmap compressImage(String imageUri) {

        String filePath = getRealPathFromURI(imageUri);
        Bitmap scaledBitmap = null;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;                      
        Bitmap bmp = BitmapFactory.decodeFile(filePath,options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;


        ExifInterface exi;
        int orien=0;
        try {
            exi = new ExifInterface(filePath);
            orien = exi.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
        } catch (IOException e1) {
            e1.printStackTrace();
        }



        float maxHeight = ScreenHeight;
        float maxWidth = ScreenWidth;


        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;     

            }
        }

        options.inSampleSize =calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16*1024];

        try{    
            bmp = BitmapFactory.decodeFile(filePath,options);
        }
        catch(OutOfMemoryError exception){
        }
        try{
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
        }
        catch(OutOfMemoryError exception){
        }

        float ratioX = actualWidth / (float)options.outWidth;
        float ratioY = actualHeight / (float)options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;

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

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



        boolean isPorait=true;
        if(((Activity)context).getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
            isPorait=false;
        }




        ExifInterface exif;
        try {
            exif = new ExifInterface(filePath);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Log.d("EXIF", "Exif: " + orientation);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                if(isPorait)
                matrix.postRotate(90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                if(!isPorait)
                matrix.postRotate(180);
                else
                matrix.postRotate(-90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 8) {
                if(isPorait)
                matrix.postRotate(270);
                else
                    matrix.postRotate(180);
                Log.d("EXIF", "Exif: " + orientation);
            }


            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
        }


        bmp.recycle();

        return scaledBitmap;

    }