Android调用手机拍照以及从相册选择照片

本文介绍了一种在Android应用中实现图片选取的方法,包括通过相机拍摄照片和从图库中选择图片的功能,并详细展示了如何处理这两种来源的图片数据。

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

  1. /*用来标识请求照相功能的activity*/  
  2.     private static final int CAMERA_WITH_DATA = 3023;  
  3.   
  4.     /*用来标识请求gallery的activity*/  
  5.     private static final int PHOTO_PICKED_WITH_DATA = 3021;  
  6.   
  7.     /*拍照的照片存储位置*/  
  8.     private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");  
  9.   
  10.     private File mCurrentPhotoFile;//照相机拍照得到的图片  

  1. private void doPickPhotoAction() {  
  2.             Context context = EditContact.this;  
  3.             // Wrap our context to inflate list items using correct theme  
  4.             final Context dialogContext = new ContextThemeWrapper(context,  
  5.                     android.R.style.Theme_Light);  
  6.             String cancel="返回";  
  7.             String[] choices;  
  8.             choices = new String[2];  
  9.             choices[0] = getString(R.string.take_photo);  //拍照  
  10.             choices[1] = getString(R.string.pick_photo);  //从相册中选择  
  11.             final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,  
  12.                     android.R.layout.simple_list_item_1, choices);  
  13.           
  14.             final AlertDialog.Builder builder = new AlertDialog.Builder(  
  15.                     dialogContext);  
  16.             builder.setTitle(R.string.attachToContact);  
  17.             builder.setSingleChoiceItems(adapter, -1,  
  18.                     new DialogInterface.OnClickListener() {  
  19.                         public void onClick(DialogInterface dialog, int which) {  
  20.                             dialog.dismiss();  
  21.                             switch (which) {  
  22.                             case 0:{  
  23.                                 String status=Environment.getExternalStorageState();  
  24.                                 if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡  
  25.                                     doTakePhoto();// 用户点击了从照相机获取  
  26.                                 }  
  27.                                 else{  
  28.                                     showToast("没有SD卡");  
  29.                                 }  
  30.                                 break;  
  31.                                   
  32.                             }  
  33.                             case 1:  
  34.                                 doPickPhotoFromGallery();// 从相册中去获取  
  35.                                 break;  
  36.                             }  
  37.                         }  
  38.                     });  
  39.             builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {  
  40.           
  41.                 @Override  
  42.                 public void onClick(DialogInterface dialog, int which) {  
  43.                     dialog.dismiss();  
  44.                 }  
  45.                   
  46.             });  
  47.             builder.create().show();  
  48.         }  
  49.     }  
  50.       
  51.     /** 
  52.     * 拍照获取图片 
  53.     *  
  54.     */  
  55.     protected void doTakePhoto() {  
  56.         try {  
  57.             // Launch camera to take photo for selected contact  
  58.             PHOTO_DIR.mkdirs();// 创建照片的存储目录  
  59.             mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名  
  60.             final Intent intent = getTakePickIntent(mCurrentPhotoFile);  
  61.             startActivityForResult(intent, CAMERA_WITH_DATA);  
  62.         } catch (ActivityNotFoundException e) {  
  63.             Toast.makeText(this, R.string.photoPickerNotFoundText,  
  64.                     Toast.LENGTH_LONG).show();  
  65.         }  
  66.     }  
  67.       
  68.     public static Intent getTakePickIntent(File f) {  
  69.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);  
  70.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));  
  71.         return intent;  
  72.     }  
  73.       
  74.     /** 
  75.     * 用当前时间给取得的图片命名 
  76.     *  
  77.     */  
  78.     private String getPhotoFileName() {  
  79.         Date date = new Date(System.currentTimeMillis());  
  80.         SimpleDateFormat dateFormat = new SimpleDateFormat(  
  81.                 "'IMG'_yyyy-MM-dd HH:mm:ss");  
  82.         return dateFormat.format(date) + ".jpg";  
  83.     }  
  84.       
  85.     // 请求Gallery程序  
  86.     protected void doPickPhotoFromGallery() {  
  87.         try {  
  88.             // Launch picker to choose photo for selected contact  
  89.             final Intent intent = getPhotoPickIntent();  
  90.             startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);  
  91.         } catch (ActivityNotFoundException e) {  
  92.             Toast.makeText(this, R.string.photoPickerNotFoundText1,  
  93.                     Toast.LENGTH_LONG).show();  
  94.         }  
  95.     }  
  96.       
  97.     // 封装请求Gallery的intent  
  98.     public static Intent getPhotoPickIntent() {  
  99.         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、付费专栏及课程。

余额充值