SmulianJulian
SmulianJulian

Reputation: 802

How to resize an image

I'm trying to resize an image that I get from my camera gallery to fit the ImageView in another activity. I am getting the image. I just need to know how to resize it to fit the ImageView.

image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent,"Select Picture"), 0);
 }
 });
}
protected void putExtras(Bundle bundle) {
        // TODO Auto-generated method stub

    }
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 0) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            image1.setImageURI(selectedImageUri);
     }}}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

my xml

         <ImageView
         android:id="@+id/image1"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="3dp"
         android:layout_weight=".1"
         android:background="@drawable/greenline" />                    

Upvotes: 0

Views: 166

Answers (3)

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

Your question only makes sense if the ImageView has a defined size the image can be fitted to. When you set the layout_width to wrap_content then it's exactly the other way round. The ImageView would wrap around the image and that doesn't seem to be what you want. So you need to use match_parent for both layout_width, layout_height or specify a concrete size like 30dip or such. Once you have that you can use android:scaleType as others mentioned.

So change your ImageView to something like this:

<ImageView
         android:id="@+id/image1"
         android:layout_width="match_parent "
         android:layout_height="match_parent "
         android:scaleType="center"
         android:layout_margin="3dp"
         android:layout_weight=".1"
         android:background="@drawable/greenline" /> 

Which scaleType you use depends on how the image should be fitted to the ImageView, with or without scaling and cropping etc.

Upvotes: 1

Emmanuel
Emmanuel

Reputation: 13223

You can try to use Bitmap.createScaledBitmap() if doing it programatically

Upvotes: 1

asenovm
asenovm

Reputation: 6517

You can set android:scaleType attribute to the ImageView. More details can be found here.

Upvotes: 1

Related Questions