In android how can i reduce my image size from 70

2020-06-29 06:11发布

I am developing an app in android that will capture a photo and store in sqlite. The image size is 70 kb but i want to store that image with 35 kb size. But i can't get any idea for this. I tried the following code but without success.

int photo_width = bm.getWidth();
int photo_height = bm.getHeight();

photo_width = 260;
photo_height = 260;

Bitmap photobitmap = Bitmap.createScaledBitmap(bm,
    photo_width, photo_height, false);

标签: android
2条回答
老娘就宠你
2楼-- · 2020-06-29 06:34

You can use Bitmap.compress() to produce a compressed version.

查看更多
祖国的老花朵
3楼-- · 2020-06-29 06:51

Use this one:

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;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

    return resizedBitmap;
}

you can use this one...This is the Best Example.....

  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;
  }
查看更多
登录 后发表回答