1

This is the code I am using to get a full sized image, which is >2mb in size and 3560 X 1876 in dimension.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    image = (ImageView) findViewById(R.id.image);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            filePath = Environment.getExternalStorageDirectory() + "/Full_"
                    + System.currentTimeMillis() + ".jpeg";
            File file = new File(filePath);
            Uri output = Uri.fromFile(file);

            Intent i = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, output);
            startActivityForResult(i, CAPTURE_IMAGE);
        }
    });
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap photo = BitmapFactory.decodeFile(filePath, options);
    image.setImageBitmap(photo);
}

Is there a way to get the image of specific size, in my case a <100kb sized image of dimensions 480 x 320.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

3 Answers3

2

You can take a look at this blog post I wrote on how to get Images from device's camera Activity:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

If you look at the end of the guide, you have this method:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH

//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;

if (height > reqHeight) 
{
    inSampleSize = Math.round((float)height / (float)reqHeight);
}
int expectedWidth = width / inSampleSize;

if (expectedWidth > reqWidth) 
{
    //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
    inSampleSize = Math.round((float)width / (float)reqWidth);
}

options.inSampleSize = inSampleSize;

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

return BitmapFactory.decodeFile(path, options);
}

you can provider there the required dimensions for your image output.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1

you have to use the inSampleSize from BitmapFactory.Options. Through it the decoder will downsample your image of the inSampleSize. For instance inSampleSize = 2 will create a bitmap of width/2 and heigth/2

int destWidth = 480;
int destHeight = 320;


while ( (bitmapWidth/2 > destWidth)  
                 || (bitmapHeight/2 > destHeight)) {
      ++inSampleSize;
      bitmapWidth /= 2;
      bitmapHeight /=2;
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • The bitmap is already saved to the path I gave using `i.putExtra(MediaStore.EXTRA_OUTPUT, output);` So, where exactly should I use this **inSampleSize**? – Archie.bpgc May 22 '13 at 07:14
  • well that is up to you. You can, for instance, downsample the bitmap everytime you need to use in your application, or decode the first time and save the downsampled copy in a specific path – Blackbelt May 22 '13 at 07:17
  • So, I should get the already saved bitmap, resize it and replace the previous bitmap? – Archie.bpgc May 22 '13 at 07:20
  • if you want to replace is your choice. " I should get the already saved bitmap", you are already doing it inside onActivityResult – Blackbelt May 22 '13 at 07:21
1

What if using a simplest way to do it. Copy this method, pass your bitmap and required height and width, get a scaled image back :)

public static Bitmap resizeBitmap(Bitmap originalBitmap, int width, int height) {

    float scaleWidth = ((float) width) / originalBitmap.getWidth();
    float scaleHeight = ((float) height) / originalBitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    return Bitmap.createBitmap(originalBitmap, 0, 0, original.getWidth(),original.getHeight(), matrix, true);
  }
Adnan
  • 5,025
  • 5
  • 25
  • 44