Android Select pictures from gallery, take pictures, crop pictures

Source: Internet
Author: User
Tags crop image

I just wrote the code.

1, first, a few, usually bright.

private static final int photo_request_carema = 1;//photo
private static final int photo_request_gallery = 2;//Select from album
private static final int photo_request_cut = 3;//result
private static final String Photo_file_name = "Temp_photo.jpg";

2 Picture Clipping
private void crop (Uri uri) {
Crop image Intent
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
Intent.putextra ("Crop", "true");
The scale of the cropping box, 1:1
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Size of output picture after cropping
Intent.putextra ("Outputx", 250);
Intent.putextra ("Outputy", 250);
Intent.putextra ("OutputFormat", "JPEG");//Picture format
Intent.putextra ("Nofacedetection", true);//Remove Face recognition
Intent.putextra ("Return-data", true);
Open an activity with a return value, the request code is Photo_request_cut
Startactivityforresult (Intent, photo_request_cut);
}

3 Gallery Select Picture
public void Gallery (view view) {
Activate the System gallery, select a picture
Intent Intent = new Intent (Intent.action_pick);
Intent.settype ("image/*");
Open an activity with a return value, the request code is Photo_request_gallery
Startactivityforresult (Intent, photo_request_gallery);
}

4 Camera Photo

public void camera (view view) {
Activating the camera
Intent Intent = new Intent ("Android.media.action.IMAGE_CAPTURE");
Determine if the memory card is usable and can be stored
if (Hassdcard ()) {
Tempfile = new File (Environment.getexternalstoragedirectory (), photo_file_name);
To create a URI from a file
Uri uri = uri.fromfile (tempfile);
Intent.putextra (Mediastore.extra_output, URI);
}
Open an activity with a return value, the request code is Photo_request_carema
Startactivityforresult (Intent, Photo_request_carema);
}

5 return results

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (Requestcode = = photo_request_gallery) {
Data returned from the album
if (data! = NULL) {
Get the full path of the picture
Uri uri = Data.getdata ();
Crop (URI);
}
} else if (Requestcode = = Photo_request_carema) {
Data returned from the camera
if (Hassdcard ()) {
Crop (Uri.fromfile (tempfile));
} else {
Toast.maketext (Mainactivity.this, "No memory card found, no photos saved! ", Toast.length_long). Show ();
}
} else if (Requestcode = = photo_request_cut) {
Data returned from the cut image
if (data! = NULL) {
Bitmap Bitmap = Data.getparcelableextra ("Data");
This.image.setImageBitmap (bitmap);
}
try {
Delete a temporary file
Tempfile.delete ();
} catch (Exception e) {
E.printstacktrace ();
}
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

6 Determine if the sdcard is being mounted

Private Boolean Hassdcard () {
if (Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {
return true;
} else {
return false;
}
}

7onCreate, do it yourself.

That's all.


Android Select pictures from gallery, take pictures, crop pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.