I. main. xml layout File
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <ImageView android: id = "@ + id/imageView" android: adjustViewBounds = "true" android: layout_gravity = "center" android: minWidth = "150dip" android: minHeight = "150dip" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/btnPhone" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "album"/> <Button android: id = "@ + id/btnTakePicture" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = ""/> </LinearLayout>
Ii. Core code
Package com. ljq. test;
Import java. io. ByteArrayOutputStream;
Import java. io. File;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageView;
Public class TestActivity extends Activity {
Private static final int NONE = 0;
Private static final int PHOTO_GRAPH = 1; // photograph
Private static final int PHOTO_ZOOM = 2; // zoom
Private static final int PHOTO_RESOULT = 3; // result
Private static final String IMAGE_UNSPECIFIED = "image /*";
Private ImageView imageView = null;
Private Button btnPhone = null;
Private Button btnTakePicture = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ImageView = (ImageView) findViewById (R. id. imageView );
BtnPhone = (Button) findViewById (R. id. btnPhone );
BtnPhone. setOnClickListener (onClickListener );
BtnTakePicture = (Button) findViewById (R. id. btnTakePicture );
BtnTakePicture. setOnClickListener (onClickListener );
}
Private final View. OnClickListener onClickListener = new View. OnClickListener (){
@ Override
Public void onClick (View v ){
If (v = btnPhone) {// obtain an image from the album
Intent intent = new Intent (Intent. ACTION_PICK, null );
Intent. setDataAndType (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED );
StartActivityForResult (intent, PHOTO_ZOOM );
} Else if (v = btnTakePicture) {// obtain the image from the photo
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
Intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (Environment
. GetExternalStorageDirectory (), "temp.jpg ")));
StartActivityForResult (intent, PHOTO_GRAPH );
}
}
};
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
If (resultCode = NONE)
Return;
// Take a photo
If (requestCode = PHOTO_GRAPH ){
// Set the file storage path
File picture = new File (Environment. getExternalStorageDirectory ()
+ "/Temp.jpg ");
StartPhotoZoom (Uri. fromFile (picture ));
}
If (data = null)
Return;
// Read the scaled image of the album
If (requestCode = PHOTO_ZOOM ){
StartPhotoZoom (data. getData ());
}
// Processing result
If (requestCode = PHOTO_RESOULT ){
Bundle extras = data. getExtras ();
If (extras! = Null ){
Bitmap photo = extras. getParcelable ("data ");
ByteArrayOutputStream stream = new ByteArrayOutputStream ();
Photo. compress (Bitmap. CompressFormat. JPEG, 75, stream); // (0-100) compressed file
// Here you can save Bitmap to the SD card, see: http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html
ImageView. setImageBitmap (photo); // display the image on the ImageView Control
}
}
Super. onActivityResult (requestCode, resultCode, data );
}
/**
* Shrink an image
*
* @ Param uri
*/
Public void startPhotoZoom (Uri uri ){
Intent intent = new Intent ("com. android. camera. action. CROP ");
Intent. setDataAndType (uri, IMAGE_UNSPECIFIED );
Intent. putExtra ("crop", "true ");
// AspectX aspectY is the aspect ratio
Intent. putExtra ("aspectX", 1 );
Intent. putExtra ("aspectY", 1 );
// OutputX outputY indicates the width and height of the cropped image.
Intent. putExtra ("outputX", 300 );
Intent. putExtra ("outputY", 500 );
Intent. putExtra ("return-data", true );
StartActivityForResult (intent, PHOTO_RESOULT );
}
}