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);
Show this picture after taking a photo
Startactivityforresult (Intent,camera_result);
Modify and display this image after taking a picture
Startactivityforresult (Intent,camera_result_cut);
catch (Exception e) {
}
}
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
1 photo display after taking photos
if (Requestcode==camera_result) {
Bitmap Bitmap = bitmapfactory.decodefile (Mphotopath, NULL);
Mimageview.setimagebitmap (bitmap);
}
2 after taking a picture, cut the photo and then show
2.1 Taking photos and tailoring
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");
Picture formats that need to be cut
In.setdataandtype (Photouriinmedia, "image/*");
Allow cut
In.putextra ("Crop", "true");
The width of the picture after clipping ImageView
In.putextra ("Outputx", 250);
The height of the picture after clipping ImageView
In.putextra ("Outputy", 250);
Set the width and height ratio of the trim 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 you cut a picture, the answer is null if "Discard"
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 on the button to take a picture"
/>
<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:minsdkvers
ion= "8" android:targetsdkversion= "ten"/> <uses-permission android:name= "Android.permission.CAMERA"/> <uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= "Android.per" Mission.
Access_network_state "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:the Me= "@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>