0

I am getting an image from the Internet and I am showing the image on to the ImageView. Please let me know is there any way in which I can compress the image(to a particular size/dimension) and show it on the Imageview, as the image is user-uploaded image. Thanks

2 Answers2

1

You can use BitmapFactory.Options class to crop image to any size.

You can use following:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 8; // 1/8th of actual image.
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

For more info, please see this.

Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • getResources(), R.id.myimage What values shall I put here ? Thanks –  Sep 03 '12 at 07:03
  • It depends upon from where you are retrieving your image.. In your case, you are getting it from internet, so you can use 'decodeStream()' with parameters like from [this](http://stackoverflow.com/questions/3118691/android-make-an-image-at-a-url-equal-to-imageviews-image) – Shrikant Ballal Sep 03 '12 at 07:08
  • I am saving that image and then again loading it as and when required –  Sep 03 '12 at 07:14
  • where are you saving that image? on sdcard or internal file or resources? – Shrikant Ballal Sep 03 '12 at 07:18
  • in the app itself InputStream input = url.openStream(); try { OutputStream output = new FileOutputStream ("data/data/com.android.muapp/logo.jpg"); try { //byte[] buffer = new byte[aReasonableSize]; int bytesRead = 0; System.out.println("Buffer Length is \t:-"+buffer.length); while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { System.out.println("inside while"); output.write(buffer, 0, bytesRead); } } finally { output.close(); } } –  Sep 03 '12 at 07:21
  • see Fedors answer from [this](http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966). See how he has created bitmap from file object. You need to create a file from your image path. then create FileInputStream and create bitmap accordingly. See Fedor's answ for more info – Shrikant Ballal Sep 03 '12 at 07:36
0

First answer is correct if you need to do it programmatically. However, ImageView also has an option to control how the image shall be cropped or re-scaled. There is an API ImageView.setScaleType(type). If you make the dimensions of your ImageView fixed, then you can control how the image is fit into the area by this method. Please see this link: http://developer.android.com/reference/android/widget/ImageView.html#setScaleType(android.widget.ImageView.ScaleType)

avepr
  • 1,865
  • 14
  • 16