-1

I am developing a application where , there is a profile page where user can set its image via camera or from gallery.

Now when user sets large image then my app stops unfortunate , i found a reason for that is , we can't set image on image View exceeds some size.

So i decided to reduce image size before setting it on image view.

Here i found many solutions but in all that it also reduces its width and height, but i don't want to reduce its width and height.

I only want to reduce its size in percentage. E.x from 1 mb to 100 kb.

How can I achieve this in Android?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jay Vyas
  • 2,674
  • 5
  • 27
  • 58
  • 1
    check this link, you may need to change the image format and reduce size in percentage http://stackoverflow.com/questions/23240432/take-and-save-picture-with-specified-size-in-kb-s/23240768#23240768 – Saqib Jul 24 '14 at 05:03
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Jay Vyas Jul 24 '14 at 05:37

1 Answers1

0

Try this way,hope this will help you to solve your problem.

public Bitmap decodeFile(String path) {
   try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, 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.decodeFile(path, o2);
    } catch (Throwable e) {
      e.printStackTrace();
    }
return null;

}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • i checked this , but here we need to pass size " final int REQUIRED_SIZE = 70;" which i dont want, i want to reduce its size in percentage – Jay Vyas Jul 24 '14 at 04:59