关闭

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

325人阅读 评论(0) 收藏 举报
Java代码  收藏代码
  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;//照相机拍照得到的图片  
 
Java代码  收藏代码
  1. private void doPickPhotoAction() {  
  2.             Context context = EditContact.this;  
  3.           
  4.             // Wrap our context to inflate list items using correct theme  
  5.             final Context dialogContext = new ContextThemeWrapper(context,  
  6.                     android.R.style.Theme_Light);  
  7.             String cancel="返回";  
  8.             String[] choices;  
  9.             choices = new String[2];  
  10.             choices[0] = getString(R.string.take_photo);  //拍照  
  11.             choices[1] = getString(R.string.pick_photo);  //从相册中选择  
  12.             final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,  
  13.                     android.R.layout.simple_list_item_1, choices);  
  14.           
  15.             final AlertDialog.Builder builder = new AlertDialog.Builder(  
  16.                     dialogContext);  
  17.             builder.setTitle(R.string.attachToContact);  
  18.             builder.setSingleChoiceItems(adapter, -1,  
  19.                     new DialogInterface.OnClickListener() {  
  20.                         public void onClick(DialogInterface dialog, int which) {  
  21.                             dialog.dismiss();  
  22.                             switch (which) {  
  23.                             case 0:{  
  24.                                 String status=Environment.getExternalStorageState();  
  25.                                 if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡  
  26.                                     doTakePhoto();// 用户点击了从照相机获取  
  27.                                 }  
  28.                                 else{  
  29.                                     showToast("没有SD卡");  
  30.                                 }  
  31.                                 break;  
  32.                                   
  33.                             }  
  34.                             case 1:  
  35.                                 doPickPhotoFromGallery();// 从相册中去获取  
  36.                                 break;  
  37.                             }  
  38.                         }  
  39.                     });  
  40.             builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {  
  41.           
  42.                 @Override  
  43.                 public void onClick(DialogInterface dialog, int which) {  
  44.                     dialog.dismiss();  
  45.                 }  
  46.                   
  47.             });  
  48.             builder.create().show();  
  49.         }  
  50.     }  
  51.       
  52.     /** 
  53.     * 拍照获取图片 
  54.     *  
  55.     */  
  56.     protected void doTakePhoto() {  
  57.         try {  
  58.             // Launch camera to take photo for selected contact  
  59.             PHOTO_DIR.mkdirs();// 创建照片的存储目录  
  60.             mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名  
  61.             final Intent intent = getTakePickIntent(mCurrentPhotoFile);  
  62.             startActivityForResult(intent, CAMERA_WITH_DATA);  
  63.         } catch (ActivityNotFoundException e) {  
  64.             Toast.makeText(this, R.string.photoPickerNotFoundText,  
  65.                     Toast.LENGTH_LONG).show();  
  66.         }  
  67.     }  
  68.       
  69.     public static Intent getTakePickIntent(File f) {  
  70.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);  
  71.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));  
  72.         return intent;  
  73.     }  
  74.       
  75.     /** 
  76.     * 用当前时间给取得的图片命名 
  77.     *  
  78.     */  
  79.     private String getPhotoFileName() {  
  80.         Date date = new Date(System.currentTimeMillis());  
  81.         SimpleDateFormat dateFormat = new SimpleDateFormat(  
  82.                 "'IMG'_yyyy-MM-dd HH:mm:ss");  
  83.         return dateFormat.format(date) + ".jpg";  
  84.     }  
  85.       
  86.     // 请求Gallery程序  
  87.     protected void doPickPhotoFromGallery() {  
  88.         try {  
  89.             // Launch picker to choose photo for selected contact  
  90.             final Intent intent = getPhotoPickIntent();  
  91.             startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);  
  92.         } catch (ActivityNotFoundException e) {  
  93.             Toast.makeText(this, R.string.photoPickerNotFoundText1,  
  94.                     Toast.LENGTH_LONG).show();  
  95.         }  
  96.     }  
  97.       
  98.     // 封装请求Gallery的intent  
  99.     public static Intent getPhotoPickIntent() {  
  100.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  101.         intent.setType("image/*");  
  102.         intent.putExtra("crop""true");  
  103.         intent.putExtra("aspectX"1);  
  104.         intent.putExtra("aspectY"1);  
  105.         intent.putExtra("outputX"80);  
  106.         intent.putExtra("outputY"80);  
  107.         intent.putExtra("return-data"true);  
  108.         return intent;  
  109.     }  
  110.       
  111.     // 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult  
  112.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  113.         if (resultCode != RESULT_OK)  
  114.             return;  
  115.         switch (requestCode) {  
  116.             case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的  
  117.                 final Bitmap photo = data.getParcelableExtra("data");  
  118.                 // 下面就是显示照片了  
  119.                 System.out.println(photo);  
  120.                 //缓存用户选择的图片  
  121.                 img = getBitmapByte(photo);  
  122.                 mEditor.setPhotoBitmap(photo);  
  123.                 System.out.println("set new photo");  
  124.                 break;  
  125.             }  
  126.             case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片  
  127.                 doCropPhoto(mCurrentPhotoFile);  
  128.                 break;  
  129.             }  
  130.         }  
  131.     }  
  132.       
  133.     protected void doCropPhoto(File f) {  
  134.         try {  
  135.             // 启动gallery去剪辑这个照片  
  136.             final Intent intent = getCropImageIntent(Uri.fromFile(f));  
  137.             startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);  
  138.         } catch (Exception e) {  
  139.             Toast.makeText(this, R.string.photoPickerNotFoundText,  
  140.                     Toast.LENGTH_LONG).show();  
  141.         }  
  142.     }  
  143.       
  144.     /**  
  145.     * Constructs an intent for image cropping. 调用图片剪辑程序  
  146.     */  
  147.     public static Intent getCropImageIntent(Uri photoUri) {  
  148.         Intent intent = new Intent("com.android.camera.action.CROP");  
  149.         intent.setDataAndType(photoUri, "image/*");  
  150.         intent.putExtra("crop""true");  
  151.         intent.putExtra("aspectX"1);  
  152.         intent.putExtra("aspectY"1);  
  153.         intent.putExtra("outputX"80);  
  154.         intent.putExtra("outputY"80);  
  155.         intent.putExtra("return-data"true);  
  156.         return intent;  
  157.     }  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16352次
    • 积分:267
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:57篇
    • 译文:0篇
    • 评论:0条
    文章分类