0

I am adding an images using the following code:

void choosePic()
{
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  startActivityForResult(intent, GALLERY_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);



     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                                                  LinearLayout.LayoutParams.WRAP_CONTENT,
                                                  LinearLayout.LayoutParams.WRAP_CONTENT);



     if(resultCode == Activity.RESULT_OK && null != data)
     {


         case GALLERY_IMAGE_ACTIVITY_REQUEST_CODE:
              try {
                  // We need to recyle unused bitmaps
                  if (myBitmap != null) {
                      myBitmap.recycle();
                  }
                  InputStream stream = getActivity().getContentResolver().openInputStream(data.getData());
                  myBitmap = BitmapFactory.decodeStream(stream);
                  stream.close();
                  photo.setVisibility(View.VISIBLE);                      
                  photo.setImageBitmap(myBitmap);
                  photo.requestLayout();
                  photo.setLayoutParams(layoutParams);

              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
             return;

         default:
             return;

         }
     }

My problem is that I am getting blank spaces (massive ones) on the top and bottem of the image after it get added. I understand that this is because of the image view resizing, but I dont get how to get rid of the blank spaces.

The layout of the image view(and it neighbours):

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
             />

        <EditText
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:autoLink="all"
            android:fadeScrollbars="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="top"
            android:hint="@string/noteshint"
            android:inputType="textMultiLine"
            android:linksClickable="true"
            android:scrollbars="vertical" />
    </LinearLayout>
</ScrollView>

Note: I have tried different values for the android:scaleType element

My question is how do I get rid of the blank space on top and button of the image view when I add a image?

Also, is there a better way to do this. I am also adding pics from the camera.

DrkStr
  • 1,752
  • 5
  • 38
  • 90

3 Answers3

1

Try this:

android:adjustViewBounds="true"

From:
https://stackoverflow.com/a/5857153/7218947

Community
  • 1
  • 1
Ahmed
  • 125
  • 1
  • 4
0

Set scale type to fitxy

android:scaleType="fitXY"

And if that space because of EditText remove this line

 <EditText
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"<-----------
            android:layout_weight="1"
            android:autoLink="all"
            android:fadeScrollbars="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="top"
            android:hint="@string/noteshint"
            android:inputType="textMultiLine"
            android:linksClickable="true"
            android:scrollbars="vertical" />
MAC
  • 15,799
  • 8
  • 54
  • 95
  • The blank spaces are gone now, but screws up the aspect ratio of the image. Is there anyway I can keep the aspect ratio of the original image? – DrkStr Apr 29 '14 at 12:24
  • If you ever found a solution to this please comment! I'm having trouble with this now. And I se what you're saying about the aspect ratio. The image becomes smushed. – ThePartyTurtle Jul 27 '15 at 21:32
0

I managed to resolve this using

android:adjustViewBounds="true"
android:scaleType="fitCenter"
D Lad
  • 146
  • 8