In Android, you can simply select and crop images,

Source: Internet
Author: User

In Android, you can simply select and crop images,

Selecting images in android is a common function. The image source is usually obtained from the camera or from the album.
First, write a simple selection button and an ImageView that displays the image.

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/bt_picture" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "get image"/> <ImageView android: id = "@ + id/picture" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center_horizontal"/> </LinearLayout>

The following is the implementation logic.

Private String [] takePhotoitems; // public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; private Button getPicture; private ImageView picture; private Uri imageUri;

Set button click events

@ Override public void onClick (View v) {takePhotoitems = new String [] {"select local image", "photo"}; imageUri = getImageUri (); new AlertDialog. builder (this ). setTitle ("set Avatar "). setItems (takePhotoitems, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {switch (which) {case 0: getImageUri (); Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. setDataAndType (MediaStore. images. media. EXTERNAL_CONTENT_URI, "image/*"); intent. setType ("image/*"); intent. putExtra ("crop", "true"); intent. putExtra ("scale", true); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); startActivityForResult (intent, CROP_PHOTO); break; case 1: // photograph getImageUri (); if (isHasSdcard () {Intent takePhotoIntent = new Intent ("android. media. action. IMAGE_CAPTURE "); takePhotoIntent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); startActivityForResult (takePhotoIntent, TAKE_PHOTO);} else {Toast. makeText (MainActivity. this, "SD card not detected", Toast. LENGTH_LONG ). show () ;}break ;}}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. dismiss ();}}). show ();}

Two tools and methods used

/*** Obtain the image path */private Uri getImageUri () {File outputImage = new File (Environment. getExternalStorageDirectory (), "tempImage.jpg"); if (outputImage. exists () {outputImage. delete ();} try {outputImage. createNewFile ();} catch (IOException e) {e. printStackTrace ();} return Uri. fromFile (outputImage );};
/*** Determine whether the SD card exists */public static boolean isHasSdcard () {String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false ;}}

Crop an image after it is obtained

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode) {        case TAKE_PHOTO:            if (resultCode == RESULT_OK) {                Intent intent = new Intent("com.android.camera.action.CROP");                intent.setDataAndType(imageUri, "image/*");                intent.putExtra("scale", true);                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                startActivityForResult(intent, CROP_PHOTO);            }            break;        case CROP_PHOTO:            if (resultCode == RESULT_OK) {                try {                    Bitmap bitmap = BitmapFactory                            .decodeStream(getContentResolver().openInputStream(                                    imageUri));                    picture.setImageBitmap(bitmap);                } catch (FileNotFoundException e) {                    e.printStackTrace();                }            }            break;        default:            break;        }    }

Related Article

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.