1

My app uses the camera to take pictures and sends the data someplace else. However, the picture sizes are too big in terms of bytes, or unecessarily big. But I'm not sure how to force the camera to take a smaller picture or after taking the picture, send a scaled down version of it.

This is how I go to the Camera screen.

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));

startActivityForResult(intent, PIC_ONE);//first picture

And then onActivityResult I have:

...
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=4;
Bitmap mBitmap = BitmapFactory.decodeFile(getPath(myURI),options);

photoView.setImageBitmap(bmp);

Which shows the user a quarter sized thumbnail of the saved image. But of course the actual image is still retains its large size.

How can I reduce the image size?

1 Answers1

1

Have a look at Reduce Bitmap size using BitmapFactory.Options.inSampleSize


You can also try createScaledBitmap()

public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) 

Since: API Level 1 Creates a new bitmap, scaled from an existing bitmap.

Parameters src The source bitmap. dstWidth The new bitmap's desired width. dstHeight The new bitmap's desired height. filter true if the source should be filtered.

Returns the new scaled bitmap.

It also reduces the file size,

You can also use Following function for resizing:

 public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

    }
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • But this bitmap is not stored in sdcard memory. It doesn't have a name or a path, which I need to send. –  Jun 03 '12 at 16:57
  • You get the resized image in a Bitmap object if you assign it to one which can be used for any further use IMHO. – Imran Rana Jun 03 '12 at 17:00
  • You can save the image to sdcard if nedded as described [here](http://stackoverflow.com/questions/10558053/save-image-to-sdcard-from-drawble-resource-on-android/10561536#10561536) – Imran Rana Jun 03 '12 at 17:03
  • Thanks. Is it possible to overwrite the original image with the smaller image? e.g. File f = new File(getPath(uri1)); and then output the smaller photo to this location? –  Jun 03 '12 at 17:19
  • Yes, you can assign the resized bitmap to the original object or simply use the resized bitmap object where you need. – Imran Rana Jun 03 '12 at 17:26