[Android] call a camera, open an album, and crop an image

Source: Internet
Author: User
Tags crop image

Private ImageView iv_user_photo; private String fileName = ""; private File tempFile; private int crop = 300; // crop the private static final int OPEN_CAMERA_CODE = 10; private static final int OPEN_GALLERY_CODE = 11; private static final int CROP_PHOTO_CODE = 12; private OnClickListener popupener witemonclick = new OnClickListener () {@ Override public void onClick (View v) {menuWindow. dismiss (); switc H (v. getId () {// photograph case R. id. btn_camera: initFile (); openCamera (); break; // album case R. id. btn_gallery: initFile (); openGallery (); break; default: break ;}}; public void initFile () {if (fileName. equals ("") {if (FileUtil. existSDCard () {String path = Environment. getExternalStorageDirectory () + File. separator + "JanuBookingOnline" + File. separator; FileUtil. mkdir (path); Logger. I ("path:" + pat H); fileName = path + "user_head_photo.jpg"; tempFile = new File (fileName);} else {CommonUitl. toast (context, "Please insert SD card") ;}}/ *** call camera */public void openCamera () {Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // enable the camera intent. putExtra ("output", Uri. fromFile (tempFile); startActivityForResult (intent, OPEN_CAMERA_CODE);}/*** open album */public void openGallery () {Intent intent = ne W Intent (Intent. ACTION_PICK); // open the album intent. setDataAndType (MediaStore. images. media. INTERNAL_CONTENT_URI, "image/*"); intent. putExtra ("output", Uri. fromFile (tempFile); startActivityForResult (intent, OPEN_GALLERY_CODE);}/*** crop image ** @ param uri */public void cropPhoto (Uri uri) {Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); intent. putExtra ("o Utput ", Uri. fromFile (tempFile); intent. putExtra ("crop", true); intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", crop); intent. putExtra ("outputY", crop); startActivityForResult (intent, CROP_PHOTO_CODE) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = 1) return; switch (requestCode) {case OPEN_CAM ERA_CODE: cropPhoto (Uri. fromFile (tempFile); break; case OPEN_GALLERY_CODE: cropPhoto (data. getData (); break; case CROP_PHOTO_CODE: try {BitmapFactory. options options = new BitmapFactory. options (); options. inSampleSize = 2; Bitmap bitmap = BitmapFactory. decodeFile (fileName, options); if (bitmap! = Null) {iv_user_photo.setImageBitmap (bitmap); CommonUitl. sharedPreferences (context, AppConstants. USER_PHOTO, fileName) ;}} catch (Exception e) {e. printStackTrace ();} break; default: break;} super. onActivityResult (requestCode, resultCode, data );}


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10025B047-0.png "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10025A919-1.png "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1002563O6-2.png "/>

This article is from the "wintitle" blog. For more information, contact the author!

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.