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); } }