0

i have this machine that i have to capture the product's image and fill other data to upload them to the server. i've been using the image capture intent but the image was being downsized by the system to 150*200 but thanks to you now i'm having the full size image. the problem is the size of the image is a big one (2-5mbs) i tried the shrink bitmap method and the picasso library but the image file is still having the same size. can you give me guidelines on how to reduce it's size (and even resolution if necessary). what i tried:

public void grabImage(ImageView imageView) {
    this.getContentResolver().notifyChange(mImageUri, null);
    ContentResolver cr = this.getContentResolver();
    Bitmap bitmap;
    try
    {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);

        //ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        Bitmap bm = ShrinkBitmap(fullpath, bitmap.getWidth(), bitmap.getHeight());
        imageView.setImageBitmap(bm);
    }
    catch (Exception e)
    {
        Log.d("This", "Failed to load", e);
    }
}

Bitmap ShrinkBitmap(String file, int width, int height){

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

    if (heightRatio > 1 || widthRatio > 1)
    {
        if (heightRatio > widthRatio)
        {
            bmpFactoryOptions.inSampleSize = 4;
        } else {
            bmpFactoryOptions.inSampleSize = 4;
        }
    }

    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    return bitmap;
}

regards,

Hussein Yassine
  • 53
  • 1
  • 1
  • 6
  • Show what you have tried. – iTurki Aug 09 '15 at 13:37
  • possible duplicate of [How to reduce an Image file size before uploading to a server](http://stackoverflow.com/questions/18573774/how-to-reduce-an-image-file-size-before-uploading-to-a-server) – Wand Maker Aug 09 '15 at 13:46

2 Answers2

0

Try this;

if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize *=2;
}
else {
bmpFactoryOptions.inSampleSize = 4;
}
}
Want2bExpert
  • 527
  • 4
  • 11
0

The following site explains very clear how to load the Image in an efficient way. This means that already when loading the Picture you do a scaling by the highest power of 2 (2,4,8,...) so that the loaded picure is still larger than the size you need. That way you just load what you actually need and use less Memory.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

(I see you use some elements of this procedure in your code, but it make little sense for me)

In a second step you can then further downsize the Picture to the actual width+heigth you need, this is simply done by: Bitmap.createScaledBitmap(bitmap_large, (int)width, (int)height, false);

Settembrini
  • 1,366
  • 3
  • 20
  • 32