This is a well tested code, so it will definitely work. Try to use below code, for cropping the image:
private CropImageView img_crop;
private int CROP_RESULT = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.back_white);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
img_crop = (CropImageView) findViewById(R.id.img_crop);
img_crop.setInitialFrameScale(0.75f);
img_crop.setAnimationEnabled(true);
img_crop.setAnimationDuration(300);
img_crop.setCropMode(CropImageView.CropMode.RATIO_3_4);
configureDefaultImageLoader(Crop_Activity.this);
Intent intent = getIntent();
Bundle b = intent.getExtras();
String path = " ";
final String type = b.getString("type");
path = b.getString("BitmapImage");
Log.e("Crop_Uri>>", path.toString());
{
try {
InputStream image_stream = null;
image_stream = getContentResolver().openInputStream(Uri.parse("file://" + path.toString()));
Bitmap bitmap1 = BitmapFactory.decodeStream(image_stream);
if (bitmap1 != null) {
bitmap1.recycle();
bitmap1 = null;
}
ImageLoader.getInstance().displayImage(ImageDownloader.Scheme.FILE.wrap(path.toString()), img_crop);
// img_crop.setImageBitmap(bitmap1);
Log.e("Image//", "//" + "yes");
} catch (Exception e) {
//handle exception
Log.e("ExceptionImage>>", "//" + e);
}
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
if (img_crop.getImageBitmap() != null) {
Bitmap crop_bitmap = img_crop.getCroppedBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
File dest = new File(getFilename());
try {
FileOutputStream out = new FileOutputStream(dest);
crop_bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent();
intent.putExtra("croped_bitmap_uri", dest.getAbsolutePath());
intent.setAction("ok");
setResult(CROP_RESULT, intent);
deleteFile();
finish();
} else {
Toast.makeText(Crop_Activity.this, "Crop Images Not Processed !", Toast.LENGTH_SHORT).show();
}
}
});
}
public String getFilename() {
File myDirectory = new File(Environment.getExternalStorageDirectory(), "Konnect_Mom/Konnect_Mom_cropped_img");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
String mImageName = "cropped_img" + ".jpg";
String uriSting = (myDirectory.getAbsolutePath() + "/" + mImageName);
return uriSting;
}
public void deleteFile() {
File file = new File(Environment.getExternalStorageDirectory() + "Konnect_Mom/Konnect_Mom_cropped_img/cropped_img.jpg");
if (file.exists()) {
file.delete();
}
}
public static void configureDefaultImageLoader(Context ctx) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.FIFO).build();
ImageLoader.getInstance().init(config);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(android.R.drawable.stat_sys_download)
.showImageForEmptyUri(android.R.drawable.ic_dialog_alert)
.showImageOnFail(android.R.drawable.stat_notify_error)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED) //filled width
.build();
}
}
Just copy the above code and paste it in a class titled "CropActivity". After that in the activity where you open "Camera" or "Gallery" to fetch images, place the below code inside the "onActivityResult()" method:
Intent i = new Intent(AddAChild.this, Crop_Activity.class);
i.putExtra("BitmapImage", filename);
startActivityForResult(i, CROP_RESULT);
That's it. Your good to go.