I have to rotate an image in an angle and crop through android crop intent.When i click a rotate button the image should rotate in an angle and display in Image View and when i click crop button the rotated image should be send to android crop intent.
In this way the angles may be changing whenever i click rotate button and corresponding rotated image displaying in Image View should be cropped through android crop action.
I tried with rotating image in all angles and it is working fine. But whenever i click crop button the rotated image is not passing to crop instead original image is passing.
this is my code
image.setImageURI(imageURI);
if (savedInstanceState != null) {
rotation = savedInstanceState.getInt("ANGLE");
}
mTempUri = HomeActivity.getOutputMediaFileUri("XXX",
"test1", MEDIA_TYPE_IMAGE, true);
{
crop.setOnClickListener (new OnClickListener() {
@Override
public void onClick(View v) {
doCrop();
}
});
rotate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Rotating.. Please wait", Toast.LENGTH_SHORT).show();
rotate_Image();
}
});
}
public void doCrop() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(imageURI);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mTempUri);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case CROP_FROM_CAMERA:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
this.finish();
break;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("ANGLE", rotation);
super.onSaveInstanceState(outState);
}
public void rotate_Image() {
rotation += 90;
rotation %= 360;
bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
imageURI);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap1 = getRotatedBitmap(bitmap);
image.setImageBitmap(bitmap1);
}
private Bitmap getRotatedBitmap(Bitmap bitmap) {
if (rotation % 360 == 0) {
return bitmap;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotation, bitmap.getWidth() / 2,
bitmap.getHeight() / 2);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
}