1

I'm really new at this and I'm trying to load profile pics from gallery and camera into ImageButton. I'm able to add image from gallery but the image does not fit completely into the ImageButton. Some space still gets left out from the imagebutton. I want the image to automatically fit into imagebutton.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.sam.sport.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/LL1" > <LinearLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" > <ImageButton android:id="@+id/profile_pic" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher" android:scaleType="fitXY" android:adjustViewBounds="true" /> </LinearLayout> </LinearLayout> </RelativeLayout> 

Activity class is as below:-

 public class MainActivity extends Activity { ImageButton ib; private static Bitmap Image = null; private static final int GALLERY = 1; private static Bitmap rotateImage = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ib = (ImageButton)findViewById(R.id.profile_pic); ib.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ib.setImageBitmap(null); if (Image != null) Image.recycle(); Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == GALLERY && resultCode != 0) { Uri mImageUri = data.getData(); try { Image = Media.getBitmap(this.getContentResolver(), mImageUri); if (getOrientation(getApplicationContext(), mImageUri) != 0) { Matrix matrix = new Matrix(); matrix.postRotate(getOrientation(getApplicationContext(), mImageUri)); if (rotateImage != null) rotateImage.recycle(); rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix, true); ib.setImageBitmap(rotateImage); } else ib.setImageBitmap(Image); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static int getOrientation(Context context, Uri photoUri) { /* it's on the external media. */ Cursor cursor = context.getContentResolver().query(photoUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); if (cursor.getCount() != 1) { return -1; } cursor.moveToFirst(); return cursor.getInt(0); } } 
3
  • use center crop in you image button xml Commented Dec 8, 2015 at 6:25
  • spaces are on top and left side ? right? Commented Dec 8, 2015 at 6:38
  • @Arslan spaces are with the whole border of ImageButton. On all 4 sides. Commented Dec 8, 2015 at 6:41

4 Answers 4

0

Convert it to Drawable then use setBackgroundDrawable insted of setImageBitmap

Drawable drawable = new BitmapDrawable(rotateImage); ib.setBackgroundDrawable(drawable); 
Sign up to request clarification or add additional context in comments.

1 Comment

Tried your way but its not happening. Still showing the imagebutton border space and the image is in the center.
0

Use below line of code for re-sizing your bitmap

 Bitmap BITMAP_IMAGE = Bitmap.createBitmap(IMAGE_VIEW.getWidth(),IMAGE_VIEW.getHeight(), Bitmap.Config.RGB_565); Bitmap resizedBitmap = Bitmap.createScaledBitmap(BITMAP_IMAGE, 100, 100, false); ////Here BITMAP_IMAGE is your bitmap which you want to resize 

10 Comments

That's not working. Did like: Bitmap resizedBitmap = Bitmap.createScaledBitmap(rotateImage, 100, 100, false); ib.setImageBitmap(resizedBitmap);
Is it not resize your bitmap image??
Yes. The image is still smaller than the actual 100dp*100dp ImageButton border.
@sam ... I have updated the answer,please check it and let me know
Is IMAGE_VIEW the ImageButton or the selected image?
|
0

Get the imageButton height and width

Bitmap bitmapScaled = Bitmap.createScaledBitmap(Image, ib_width, ib_height, true); Drawable drawable = new BitmapDrawable(bitmapScaled); ib.setBackgroundDrawable(drawable); 

Keep in mind about OOM exception.

To get imageButton height and width look into this and this

Comments

0

I've got the perfect solution. To resize imageview I used the below code as provided in the [Android ImageView adjusting parent's height and fitting width

public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ Drawable d = getDrawable(); if(d!=null){ // ceil not round - avoid thin vertical gaps along the left/right edges int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } }

]1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.