Android photos are cropped before the image is displayed.

Source: Internet
Author: User
Tags dateformat

MainActivity is as follows:

Package cn. testcamera; import java. io. file; import java. text. simpleDateFormat; import java. util. date; import android. app. activity; import android. content. contentResolver; import android. content. contentUris; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. provider. MediaStore; import android. view. view; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity {private Button mButton; private ImageView mImageView; private File mPhotoFile; private String mPhotoPath; private Uri mPhotoOnSDCardUri; public final static int CAMERA_RESULT = 777; public final static int CAMERA_RESULT_CUT = 888; public final static int CAMERA_RESU LT_CUT_OVER = 999; public final static String TAG = "xx"; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new ButtonOnClickListener (); mImageView = (ImageView) findViewById (R. id. imageView);} private class ButtonOnClickListener implements View. onClickListener {Public void onClick (View v) {try {Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); mPhotoPath =" mnt/sdcard/DCIM/Camera/"+ getPhotoFileName (); mPhotoFile = new File (mPhotoPath); if (! MPhotoFile. exists () {mPhotoFile. createNewFile ();} mPhotoOnSDCardUri = Uri. fromFile (mPhotoFile); intent. putExtra (MediaStore. EXTRA_OUTPUT, mPhotoOnSDCardUri); // This image is displayed after the image is taken // startActivityForResult (intent, CAMERA_RESULT); // After the image is taken, it is modified before startActivityForResult (intent, CAMERA_RESULT_CUT );} catch (Exception e) {}}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onA CtivityResult (requestCode, resultCode, data); // 1. After taking the photo, the image is displayed if (requestCode = CAMERA_RESULT) {Bitmap bitmap = BitmapFactory. decodeFile (mPhotoPath, null); mImageView. setImageBitmap (bitmap);} // 2 take a photo and cut the photo. Then, the image is displayed // 2.1 and the image is cropped if (requestCode = CAMERA_RESULT_CUT) {Intent intent = new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE, mPhotoOnSDCardUri); sendBroadcast (intent); try {Thread. sleep (2000);} catch (Interrupted Exception e) {e. printStackTrace ();} Uri systemImageUri = MediaStore. images. media. EXTERNAL_CONTENT_URI; ContentResolver contentResolver = getContentResolver (); Cursor cursor = contentResolver. query (systemImageUri, null, MediaStore. images. media. DISPLAY_NAME + "= '" + mPhotoFile. getName () + "'", null, null); Uri photoUriInMedia = null; if (cursor! = Null & cursor. getCount ()> 0) {cursor. moveToLast (); long id = cursor. getLong (0); photoUriInMedia = ContentUris. withAppendedId (systemImageUri, id);} cursor. close (); Intent in = new Intent ("com. android. camera. action. CROP "); // The image format to be dropped in. setDataAndType (photoUriInMedia, "image/*"); // allow in. putExtra ("crop", "true"); // specifies the width of the image displayed in the cropped ImageView. putExtra ("outputX", 250); // specifies the height of the image displayed in the cropped ImageView. putExtra ("outputY ", 250); // set the width and height of the cropping box in. putExtra ("aspectX", 1); in. putExtra ("aspectY", 1); in. putExtra ("return-data", true); startActivityForResult (in, CAMERA_RESULT_CUT_OVER);} // 2.2 display if (requestCode = CAMERA_RESULT_CUT_OVER) {// when cropping an image, if "discard" is returned, the data returned is nullif (data! = Null) {Bitmap bitmap = (Bitmap) data. getExtras (). get ("data"); mImageView. setImageBitmap (bitmap) ;}} private String getPhotoFileName () {Date date = new Date (System. currentTimeMillis (); SimpleDateFormat dateFormat = new SimpleDateFormat ("'img '_ yyyyMMdd_HHmmss"); return dateFormat. format (date) + ". jpg ";}}

Main. xml is as follows:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: id = "@ + id/button" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "click to take a photo"/> <ImageView android: id = "@ + id/imageView" android: layout_below = "@ id/button" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </RelativeLayout>

Manifest. xml is as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.testcamera"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="10" />    <uses-permission android:name="android.permission.CAMERA"/>    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />      <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:launchMode="singleTask"            android:label="@string/title_activity_main" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

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.