转来的··调用系统相机拍照和调用相册

本文介绍如何在Android应用中实现相机拍照、照片选取及照片剪切功能,包括使用Intent进行界面交互、图片文件命名、裁剪操作及数据处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值