0

How to compress the image in android application.

My application selects photo from gallery using intent and sets the photo as background for the layout,This works fine when i select a photo of small pixel,bit photo taken from camera it does not fit the screen.

how to compress the photo or image.

user3009917
  • 113
  • 9
  • possible duplicate of [Android. Scale image keeping its aspect ratio in background drawable](http://stackoverflow.com/questions/9891065/android-scale-image-keeping-its-aspect-ratio-in-background-drawable) – CSmith Nov 19 '13 at 18:15
  • You probably looking for http://stackoverflow.com/questions/2521959/how-to-scale-an-image-in-imageview-to-keep-the-aspect-ratio – CodeWarrior Nov 19 '13 at 18:23

2 Answers2

1

Use following intent to get image of required size from Gallery.

         Intent photoPickerIntent = new Intent(
                        Intent.ACTION_PICK,
                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // photoPickerIntent.setType("image/*");
                photoPickerIntent.putExtra("crop", "true");
                photoPickerIntent.putExtra("outputX", 512);
                photoPickerIntent.putExtra("outputY", 512);
                photoPickerIntent.putExtra("aspectX", 1);
                photoPickerIntent.putExtra("aspectY", 1);
                photoPickerIntent.putExtra("scale", true);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "tt.jpg");
                // imgpath=f.getAbsolutePath();
                System.err.println("Path$$$$$$$$$ " + f.getAbsolutePath());
                photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(f));
                photoPickerIntent.putExtra("outputFormat",
                        Bitmap.CompressFormat.PNG.toString());
                startActivityForResult(photoPickerIntent, SELECT_FILE);
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
0

To measure the width and height of your Imageview at run time

ViewTreeObserver vto = mUserPicImageView.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {

            if (mImageHeight == 0) {
                mImageHeight = mUserPicImageView.getMeasuredHeight();
                mImageWidth = mUserPicImageView.getMeasuredWidth();
                Log.e("mImageHeight", mImageHeight + "");

            }
            return true;
        }
    });

Create image of the above width and height

 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,mImageHeight,mImageWidth, true);

Set the image to ImageView

mUserPicImageView.setImageBitmap(scaledBitmap);
Anand Prakash
  • 381
  • 2
  • 9