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_RESULT_CUT_OVER = 999;
Public final static String TAG = "xx ";
@ Override
Public 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 );
// Display the image after taking the photo
// StartActivityForResult (intent, CAMERA_RESULT );
// Modify the image before displaying it.
StartActivityForResult (intent, CAMERA_RESULT_CUT );
} Catch (Exception e ){
}
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
// 1. display the photo after taking the photo
If (requestCode = CAMERA_RESULT ){
Bitmap bitmap = BitmapFactory. decodeFile (mPhotoPath, null );
MImageView. setImageBitmap (bitmap );
}
// 2. Cut the photo and display it.
// 2.1 photograph and crop
If (requestCode = CAMERA_RESULT_CUT ){
Intent intent = new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE, mPhotoOnSDCardUri );
SendBroadcast (intent );
Try {
Thread. sleep (2000 );
} Catch (InterruptedException 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 reduced
In. setDataAndType (photoUriInMedia, "image /*");
// Cut allowed
In. putExtra ("crop", "true ");
// Specifies the image width when the cropped ImageView is displayed.
In. putExtra ("outputX", 250 );
// Height of the image when the cropped ImageView is displayed
In. putExtra ("outputY", 250 );
// Set the width/height ratio of the cropping box.
In. putExtra ("aspectX", 1 );
In. putExtra ("aspectY", 1 );
In. putExtra ("return-data", true );
StartActivityForResult (in, CAMERA_RESULT_CUT_OVER );
}
// Display 2.2
If (requestCode = CAMERA_RESULT_CUT_OVER ){
// When you cut an image, if you give up, the data returned is null.
If (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>