In my previous post i explained how to capture photos using Android camera today in this post i am going to
explain how to crop image loading from gallery for this task following are my source codes
CropActivity.java
package com.example.crop;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CropActivity extends Activity {
protected static final int PICK_FROM_GALLERY = 1;
Button btcrop;
ImageView imgview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
btcrop= (Button) findViewById(R.id.btcrop);
imgview=(ImageView)findViewById(R.id.quickContactBadge1);
btcrop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
}
}
Above code is my source code of crop project when ever i press crop button it will open image from gallery it show preview here i gave fixed size of length of image after that output show on ImageView
activity_crop.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btcrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="124dp"
android:layout_marginTop="128dp"
android:text="Crop" />
<QuickContactBadge
android:id="@+id/quickContactBadge1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btcrop"
android:layout_centerVertical="true" />
</RelativeLayout>
Above code is my layout file it have two fields one is button and one-more is QuickContactBadge it like image view only
when ever i press button it will open image from gallery after that output show's on image view