android调用相机拍照,照片选取,照片剪切 - [android]
001
private
void
doPickPhotoAction()
{
002 Context context = EditContact . this;
003
004 // Wrap our context to inflate list items using correct theme
005 final Context dialogContext = new ContextThemeWrapper( context ,
006 android . R . style . Theme_Light);
007 String cancel = "返回";
008 String [] choices;
009 choices = new String [ 2 ];
010 choices [ 0 ] = getString( R . string . take_photo); //拍照
011 choices [ 1 ] = getString( R . string . pick_photo); //从相册中选择
012 final ListAdapter adapter = new ArrayAdapter < String >( dialogContext ,
013 android . R . layout . simple_list_item_1 , choices);
014
015 final AlertDialog . Builder builder = new AlertDialog . Builder(
016 dialogContext);
017 builder . setTitle( R . string . attachToContact);
018 builder . setSingleChoiceItems( adapter , - 1 ,
019 new DialogInterface . OnClickListener() {
020 public void onClick( DialogInterface dialog , int which) {
021 dialog . dismiss();
022 switch ( which) {
023 case 0 :{
024 String status = Environment . getExternalStorageState();
025 if( status . equals( Environment . MEDIA_MOUNTED )){ //判断是否有SD卡
026 doTakePhoto(); // 用户点击了从照相机获取
027 }
028 else {
029 showToast( "没有SD卡");
030 }
031 break;
032
033 }
034 case 1 :
035 doPickPhotoFromGallery(); // 从相册中去获取
036 break;
037 }
038 }
039 });
040 builder . setNegativeButton( cancel , new DialogInterface . OnClickListener() {
041
042 @Override
043 public void onClick( DialogInterface dialog , int which) {
044 dialog . dismiss();
045 }
046
047 });
048 builder . create (). show();
049 }
050 }
051
052 /**
053 * 拍照获取图片
054 *
055 */
056 protected void doTakePhoto() {
057 try {
058 // Launch camera to take photo for selected contact
059 PHOTO_DIR . mkdirs(); // 创建照片的存储目录
060 mCurrentPhotoFile = new File( PHOTO_DIR , getPhotoFileName()); // 给新照的照片文件命名
061 final Intent intent = getTakePickIntent( mCurrentPhotoFile);
062 startActivityForResult( intent , CAMERA_WITH_DATA);
063 } catch ( ActivityNotFoundException e) {
064 Toast . makeText( this , R . string . photoPickerNotFoundText ,
065 Toast . LENGTH_LONG ). show();
066 }
067 }
068
069 public static Intent getTakePickIntent( File f) {
070 Intent intent = new Intent( MediaStore . ACTION_IMAGE_CAPTURE , null);
071 intent . putExtra( MediaStore . EXTRA_OUTPUT , Uri . fromFile( f));
072 return intent;
073 }
074
075 /**Eclipse 安装 Google Android 插件 * 用当前时间给取得的图片命名
076 *
077 */
078 private String getPhotoFileName() {
079 Date date = new Date( System . currentTimeMillis());
080 SimpleDateFormat dateFormat = new SimpleDateFormat(
081 "'IMG'_yyyy-MM-dd HH:mm:ss");
082 return dateFormat . format( date) + ".jpg";
083 }
084
085 // 请求Gallery程序
086 protected void doPickPhotoFromGallery() {
087 try {
088 // Launch picker to choose photo for selected contact
089 final Intent intent = getPhotoPickIntent();
090 startActivityForResult( intent , PHOTO_PICKED_WITH_DATA);
091 } catch ( ActivityNotFoundException e) {
092 Toast . makeText( this , R . string . photoPickerNotFoundText1 ,
093 Toast . LENGTH_LONG ). show();
094 }
095 }
096
097 // 封装请求Gallery的intent
098 public static Intent getPhotoPickIntent() {
099 Intent intent = new Intent( Intent . ACTION_GET_CONTENT , null);
100 intent . setType( "image/*");
101 intent . putExtra( "crop" , "true");
102 intent . putExtra( "aspectX" , 1);
103 intent . putExtra( "aspectY" , 1);
104 intent . putExtra( "outputX" , 80);
105 intent . putExtra( "outputY" , 80);
106 intent . putExtra( "return-data" , true);
107 return intent;
108 }
109
110 // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
111 protected void onActivityResult( int requestCode , int resultCode , Intent data) {
112 if ( resultCode != RESULT_OK)
113 return;
114 switch ( requestCode) {
115 case PHOTO_PICKED_WITH_DATA: { // 调用Gallery返回的
116 final Bitmap photo = data . getParcelableExtra( "data");
117 // 下面就是显示照片了
118 System . out . println( photo);
119 //缓存用户选择的图片
120 img = getBitmapByte( photo);
121 mEditor . setPhotoBitmap( photo);
122 System . out . println( "set new photo");
123 break;
124 }
125 case CAMERA_WITH_DATA: { // 照相机程序返回的,再次调用图片剪辑程序去修剪图片
126 doCropPhoto( mCurrentPhotoFile);
127 break;
128 }
129 }
130 }
131
132 protected void doCropPhoto( File f) {
133 try {
134 // 启动gallery去剪辑这个照片
135 final Intent intent = getCropImageIntent( Uri . fromFile( f));
136 startActivityForResult( intent , PHOTO_PICKED_WITH_DATA);
137 } catch ( Exception e) {
138 Toast . makeText( this , R . string . photoPickerNotFoundText ,
139 Toast . LENGTH_LONG ). show();
140 }
141 }
142
143 /**
144 * Constructs an intent for image cropping. 调用图片剪辑程序
145 */
146 public static Intent getCropImageIntent( Uri photoUri) {
147 Intent intent = new Intent( "com.android.camera.action.CROP");
148 intent . setDataAndType( photoUri , "image/*");
149 intent . putExtra( "crop" , "true");
150 intent . putExtra( "aspectX" , 1);
151 intent . putExtra( "aspectY" , 1);
152 intent . putExtra( "outputX" , 80);
153 intent . putExtra( "outputY" , 80);
154 intent . putExtra( "return-data" , true);
155 return intent;
156 }
002 Context context = EditContact . this;
003
004 // Wrap our context to inflate list items using correct theme
005 final Context dialogContext = new ContextThemeWrapper( context ,
006 android . R . style . Theme_Light);
007 String cancel = "返回";
008 String [] choices;
009 choices = new String [ 2 ];
010 choices [ 0 ] = getString( R . string . take_photo); //拍照
011 choices [ 1 ] = getString( R . string . pick_photo); //从相册中选择
012 final ListAdapter adapter = new ArrayAdapter < String >( dialogContext ,
013 android . R . layout . simple_list_item_1 , choices);
014
015 final AlertDialog . Builder builder = new AlertDialog . Builder(
016 dialogContext);
017 builder . setTitle( R . string . attachToContact);
018 builder . setSingleChoiceItems( adapter , - 1 ,
019 new DialogInterface . OnClickListener() {
020 public void onClick( DialogInterface dialog , int which) {
021 dialog . dismiss();
022 switch ( which) {
023 case 0 :{
024 String status = Environment . getExternalStorageState();
025 if( status . equals( Environment . MEDIA_MOUNTED )){ //判断是否有SD卡
026 doTakePhoto(); // 用户点击了从照相机获取
027 }
028 else {
029 showToast( "没有SD卡");
030 }
031 break;
032
033 }
034 case 1 :
035 doPickPhotoFromGallery(); // 从相册中去获取
036 break;
037 }
038 }
039 });
040 builder . setNegativeButton( cancel , new DialogInterface . OnClickListener() {
041
042 @Override
043 public void onClick( DialogInterface dialog , int which) {
044 dialog . dismiss();
045 }
046
047 });
048 builder . create (). show();
049 }
050 }
051
052 /**
053 * 拍照获取图片
054 *
055 */
056 protected void doTakePhoto() {
057 try {
058 // Launch camera to take photo for selected contact
059 PHOTO_DIR . mkdirs(); // 创建照片的存储目录
060 mCurrentPhotoFile = new File( PHOTO_DIR , getPhotoFileName()); // 给新照的照片文件命名
061 final Intent intent = getTakePickIntent( mCurrentPhotoFile);
062 startActivityForResult( intent , CAMERA_WITH_DATA);
063 } catch ( ActivityNotFoundException e) {
064 Toast . makeText( this , R . string . photoPickerNotFoundText ,
065 Toast . LENGTH_LONG ). show();
066 }
067 }
068
069 public static Intent getTakePickIntent( File f) {
070 Intent intent = new Intent( MediaStore . ACTION_IMAGE_CAPTURE , null);
071 intent . putExtra( MediaStore . EXTRA_OUTPUT , Uri . fromFile( f));
072 return intent;
073 }
074
075 /**Eclipse 安装 Google Android 插件 * 用当前时间给取得的图片命名
076 *
077 */
078 private String getPhotoFileName() {
079 Date date = new Date( System . currentTimeMillis());
080 SimpleDateFormat dateFormat = new SimpleDateFormat(
081 "'IMG'_yyyy-MM-dd HH:mm:ss");
082 return dateFormat . format( date) + ".jpg";
083 }
084
085 // 请求Gallery程序
086 protected void doPickPhotoFromGallery() {
087 try {
088 // Launch picker to choose photo for selected contact
089 final Intent intent = getPhotoPickIntent();
090 startActivityForResult( intent , PHOTO_PICKED_WITH_DATA);
091 } catch ( ActivityNotFoundException e) {
092 Toast . makeText( this , R . string . photoPickerNotFoundText1 ,
093 Toast . LENGTH_LONG ). show();
094 }
095 }
096
097 // 封装请求Gallery的intent
098 public static Intent getPhotoPickIntent() {
099 Intent intent = new Intent( Intent . ACTION_GET_CONTENT , null);
100 intent . setType( "image/*");
101 intent . putExtra( "crop" , "true");
102 intent . putExtra( "aspectX" , 1);
103 intent . putExtra( "aspectY" , 1);
104 intent . putExtra( "outputX" , 80);
105 intent . putExtra( "outputY" , 80);
106 intent . putExtra( "return-data" , true);
107 return intent;
108 }
109
110 // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
111 protected void onActivityResult( int requestCode , int resultCode , Intent data) {
112 if ( resultCode != RESULT_OK)
113 return;
114 switch ( requestCode) {
115 case PHOTO_PICKED_WITH_DATA: { // 调用Gallery返回的
116 final Bitmap photo = data . getParcelableExtra( "data");
117 // 下面就是显示照片了
118 System . out . println( photo);
119 //缓存用户选择的图片
120 img = getBitmapByte( photo);
121 mEditor . setPhotoBitmap( photo);
122 System . out . println( "set new photo");
123 break;
124 }
125 case CAMERA_WITH_DATA: { // 照相机程序返回的,再次调用图片剪辑程序去修剪图片
126 doCropPhoto( mCurrentPhotoFile);
127 break;
128 }
129 }
130 }
131
132 protected void doCropPhoto( File f) {
133 try {
134 // 启动gallery去剪辑这个照片
135 final Intent intent = getCropImageIntent( Uri . fromFile( f));
136 startActivityForResult( intent , PHOTO_PICKED_WITH_DATA);
137 } catch ( Exception e) {
138 Toast . makeText( this , R . string . photoPickerNotFoundText ,
139 Toast . LENGTH_LONG ). show();
140 }
141 }
142
143 /**
144 * Constructs an intent for image cropping. 调用图片剪辑程序
145 */
146 public static Intent getCropImageIntent( Uri photoUri) {
147 Intent intent = new Intent( "com.android.camera.action.CROP");
148 intent . setDataAndType( photoUri , "image/*");
149 intent . putExtra( "crop" , "true");
150 intent . putExtra( "aspectX" , 1);
151 intent . putExtra( "aspectY" , 1);
152 intent . putExtra( "outputX" , 80);
153 intent . putExtra( "outputY" , 80);
154 intent . putExtra( "return-data" , true);
155 return intent;
156 }