Androidのデフォルトのトリミング機能を使用して、イメージビューに存在するビットマップをトリミングし、そのトリミングされたビットマップを現在のイメージビューに設定したい.これは私のコードですが、機能しません.助けが必要です...
ImageView iv;
final int CROP_PIC_REQUEST_CODE = 1;
String path;
Uri getUri;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
bitmap = Bitmap.createBitmap(iv.getWidth(),iv.getHeight(),Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
iv.draw(canvas);
iv.setBackgroundResource(R.drawable.ally);
// to get Uri of the imageview
getUri = getImageUri(this, bitmap);
//android cropping function
doCrop();
}
public void doCrop() {
try {
// cropping intent
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// Set data and type
cropIntent.setDataAndType(getUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
//start the activity for cropping
startActivityForResult(cropIntent, CROP_PIC_REQUEST_CODE);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CROP_PIC_REQUEST_CODE) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
bitmap= extras.getParcelable("data");
//set the bitmap to imageview
iv.setImageBitmap(bitmap);
}
}
}