1

I have a a pice of code:

Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 

finally bitmap has the image from camera (i am taking image from a high resolution camera ) so size may be 10mb.


What i am trying to do::

I am trying to set the bitmap in imageview as below

portfolioPicImgId.setImageBitmap(bitmap);
  • Before that i want to compress the image into 300kb how can I achieve this !
  • I have seen other stackoverflow answers but how to specify exactly to 300kb
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • @ Midhun Krishna .... So I can learn how to specify exact size ! and adapt myself to other requirements ! – Devrath Dec 11 '14 at 04:48

3 Answers3

1

Its hard to get exactly 300kb. It depends on quality, every pixel contains 4(32 bit) or 2(16bit) bytes that means that image should contain 75000 pixels(in 32bit image). Next step get image proportion for simplicity take proportions of an image 1:1 - sqrt(75000) ~ 274 pixels and we get 274x274 pixels would be ~300kb in ram.

All you need to do with this data its specify width and height in options.

Stepango
  • 4,721
  • 3
  • 31
  • 44
0

Have you tried this? :

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

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

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

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • `REQUIRED_SIZE= 70` means is it `70kb` and what is `scale=1` ...can you give some info on this ! – Devrath Dec 11 '14 at 05:10
  • @Devrath, refer to this answer for more information: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Vikasdeep Singh Dec 11 '14 at 05:23
0

Just pass the path of image in below function

    public Bitmap get_Reduce_bitmap_Picture(String imagePath) {

    int ample_size = 16;
     // change ample_size to 32 or any power of 2 to increase or decrease bitmap size of image


    Bitmap bitmap = null;
    BitmapFactory.Options bitoption = new BitmapFactory.Options();
    bitoption.inSampleSize = ample_size;

    Bitmap bitmapPhoto = BitmapFactory.decodeFile(imagePath, bitoption);

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
    } catch (IOException e) {
        // Auto-generated catch block
        e.printStackTrace();
    }
    int orientation = exif
            .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    Matrix matrix = new Matrix();

    if ((orientation == 3)) {
        matrix.postRotate(180);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 6) {
        matrix.postRotate(90);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 8) {
        matrix.postRotate(270);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else {
        matrix.postRotate(0);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    }

    return bitmap;

}