0

I was trying to save images into the SQLite database, but even I compress and rescale my bitmap, the file was still too big, especially when you capture a image from the phone's camera.

Is there anyway to zip the bytearray/bitmap into a specific size like from 4mb to 500kb? If yes, how can I do that?

private void uriToFile(Uri uri, File sdImageMainDirectory) throws IOException {

        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        OutputStream stream = new FileOutputStream(sdImageMainDirectory);

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int maxSize = 10;

        float bitmapRatio = (float) bitmapWidth / (float) bitmapHeight;

        if(bitmapRatio > 0){
            bitmapWidth = maxSize;
            bitmapHeight = (int) (bitmapWidth / bitmapRatio);
        }
        else{
            bitmapHeight = maxSize;
            bitmapWidth = (int) (bitmapHeight * bitmapRatio);
        }
        bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
        stream.flush();
        stream.close();
}
fadden
  • 51,356
  • 5
  • 116
  • 166
Johnny Cheuk
  • 237
  • 2
  • 15
  • So you want to reduce the size to 1/8th? Seems a bit extreme, no? You can resize the image, but compression isn't magic. – 323go Dec 18 '15 at 03:18
  • I just need the photo to be able to store into a single row from the database since each row can only fits 2MB, but I have already resize and compress the bitmap it still over 2MB – Johnny Cheuk Dec 18 '15 at 03:19
  • Then make it smaller. Once again, zip isn't magic. And most image formats are already compressed, making them very poor candidates for further compression. It's generally a bad idea to store large binary data in a database. Consider just storing them as files in the private folder, and keeping a reference in your database. – 323go Dec 18 '15 at 03:21
  • @323go I've posted my method to zip a image, I think it's small enough already – Johnny Cheuk Dec 18 '15 at 03:25
  • There is no "zip" in the code you posted. You're just compressing the image. – 323go Dec 18 '15 at 03:28
  • @323go Sorry I think I used the wrong word. So how can I compress a image from 400mb to less than 500kb – Johnny Cheuk Dec 18 '15 at 03:30

2 Answers2

1

You're not actually compressing the scaled image, but rather, the original one. Change the last four lines to this:

Bitmap scaled = bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
scaled.compress(Bitmap.CompressFormat.JPEG, 30, stream);
stream.flush();
stream.close();

createScaledBitmap() returns the scaled bitmap, and you just lost the result.

You should be able to set maxSize to 1000 and see file sizes below 500k.

323go
  • 14,143
  • 6
  • 33
  • 41
1

you can use these codes also

 String[] filePathColumn = {MediaStore.Images.Media.DATA};
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();

  **Bitmap bitmapImage = BitmapFactory.decodeFile(img_Decodable_Str);
  Bitmap converetdImage = getResizedBitmap(bitmapImage, 500);**
sKv
  • 66
  • 6