-1

so i have a profile activity with a profile image. Now the image that was taken vertically. And I have horizontal image views. When i grab images onActivityResult() how do i "square" the image. By square the image, I mean, I want each image's width to be the width of the screen (and since its a square, height = image width = screen width).

In onActivityResult() i get the image from my gallery and camera back as a string path (which i can convert to a bitmap). Additionally, when i get images i get them from my backend using the Glide library and a url.

Glide.with(context).load("www.mybackendToImagePath").centerCrop().into(newImageView);

So, I need to convert that image path into a bitmap and then somehow square that bitmap with my screen's width. I'm not sure what code allows me to:

  1. get the dimensions of the device.
  2. transform's the image's width and height, to the width of the device.

Hope you guys can help!

Heres what my profile activity looks like.

enter image description here

TheQ
  • 1,949
  • 10
  • 38
  • 63

1 Answers1

2

You can use this https://github.com/Yalantis/uCrop library to crop image you get from camera or gallery. Include the library as local library project

 allprojects {
 repositories {
  jcenter()
  maven { url "https://jitpack.io" }
 }
}

Add Dependencies

compile 'com.github.yalantis:ucrop:2.2.1'

or

compile 'com.github.yalantis:ucrop:2.2.1-native'

Add UCropActivity into your AndroidManifest.xml

<activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

Now pass the URI of your image as sourceUri in following code and destinationUri will be where you want to place your cropped image

UCrop.of(sourceUri, destinationUri)
.withAspectRatio(1, 1)//1, 1 is the ratio b/w width and height of image i.e. square you can pass any ratio here
.withMaxResultSize(maxWidth, maxHeight)
.start(context);

Override onActivityResult method and handle uCrop result.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        final Uri resultUri = UCrop.getOutput(data);
    } else if (resultCode == UCrop.RESULT_ERROR) {
        final Throwable cropError = UCrop.getError(data);
    }
}
Muhammad Hamza Shahid
  • 956
  • 1
  • 15
  • 23