// layout click listeners
llSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
utils = new Utils(getApplicationContext());
bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
File f = new File(utils.saveImageToSDCard(bitmap));
quri = Uri.fromFile( f);
h = bitmap.getHeight();
w = bitmap.getWidth();
performCrop(quri, h, w);
}
});
. Это обработчик нажатия для кнопки, которая выполняет performcrop();
public void performCrop(Uri picUri, int height, int width){
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", height);
cropIntent.putExtra("aspectY", width);
//indicate X and Y
cropIntent.putExtra("outputX", height);
cropIntent.putExtra("outputY", width);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 1);
}
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();
}
}
Это код метода performCorp();
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
//get the cropped bitmap
if (data != null) {
Bundle extras = data.getExtras();
utils = new Utils(getApplicationContext());
Bitmap thePic = extras.getParcelable("data");
utils.setAsWallpaper(thePic);
}
}
}
Код для OnActivityResult();