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; } }