public class ImageCroppMainActivity extends Activity implements OnClickListener{
final int CAMERA_CAPTURE = 1;
//keep track of cropping intent
final int PIC_CROP = 2;
//captured picture uri
private Uri picUri;
final int PICK_IMAGE = 3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_cropp_main);
//retrieve a reference to the UI button
Button captureBtn = (Button)findViewById(R.id.capture_btn);
//handle button clicks
captureBtn.setOnClickListener(this);
Button browseBtn = (Button)findViewById(R.id.browse_btn);
//handle button clicks
browseBtn.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
//use standard intent to capture an image
Intent captureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
else if(v.getId() == R.id.browse_btn)
{
//use standard intent to capture an image
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//we will handle the returned data in onActivityResult
startActivityForResult(intent, PICK_IMAGE);
}
}
/**
* Handle user returning from both capturing and cropping the image
*/
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if(requestCode == CAMERA_CAPTURE){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
}
//user is returning from cropping the image
else if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView =
(ImageView)findViewById(R.id.picture);
//display the returned cropped image
MediaStore.Images.Media.insertImage(getContentResolver(), thePic, "" , "");
picView.setImageBitmap(thePic);
}
else if(requestCode == PICK_IMAGE)
{
picUri = data.getData();
//carry out the crop operation
performCrop();
}}}
/**
* Helper method to carry out crop operation
*/
private void performCrop(){
//take care of exceptions
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new
Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "false");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
//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();
}
}
}
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Try this add to your OnActivityResult
It can be done in only 3 steps -
1)Android Manifest file
2)SimpleCameraGalleryDemo Code
3) main.xml File
I hope it is helpful to you.
Please check the following link it will helps you to crop the image.
https://github.com/lorensiuswlt/AndroidImageCrop .
I think it will help you. let me know your status.