1 private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 2 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 3 private static final int PHOTO_REQUEST_CUT = 3;// 结果 4 private File tempFile = new File(Environment.getExternalStorageDirectory(), 5 getPhotoFileName()); 6 7 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 8 switch (requestCode) { 9 case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用 10 startPhotoZoom(Uri.fromFile(tempFile)); 11 break; 12 case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时 13 // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同 14 if (data != null) { 15 System.out.println("11================"); 16 startPhotoZoom(data.getData()); 17 } else { 18 System.out.println("================"); 19 } 20 break; 21 case PHOTO_REQUEST_CUT:// 返回的结果 22 if (data != null) 23 // setPicToView(data); 24 sentPicToNext(data); 25 break; 26 } 27 super.onActivityResult(requestCode, resultCode, data); 28 }
1 // 使用系统当前日期加以调整作为照片的名称 2 private String getPhotoFileName() { 3 Date date = new Date(System.currentTimeMillis()); 4 SimpleDateFormat dateFormat = new SimpleDateFormat( 5 "'IMG'_yyyyMMdd_HHmmss"); 6 return dateFormat.format(date) + ".jpg"; 7 }
调用系统拍照功能:
1 Intent cameraintent = new Intent( 2 MediaStore.ACTION_IMAGE_CAPTURE); 3 // 指定调用相机拍照后照片的储存路径 4 cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, 5 Uri.fromFile(tempFile)); 6 startActivityForResult(cameraintent, 7 PHOTO_REQUEST_TAKEPHOTO);
调用系统相册功能:
1 Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
2 getAlbum.setType("image/*");
3 startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);
调用系统裁剪功能:
1 private void startPhotoZoom(Uri uri) { 2 Intent intent = new Intent("com.android.camera.action.CROP"); 3 intent.setDataAndType(uri, "image/*"); 4 // crop为true是设置在开启的intent中设置显示的view可以剪裁 5 intent.putExtra("crop", "true"); 6 7 // aspectX aspectY 是宽高的比例 8 intent.putExtra("aspectX", 1); 9 intent.putExtra("aspectY", 1); 10 11 // outputX,outputY 是剪裁图片的宽高 12 intent.putExtra("outputX", 300); 13 intent.putExtra("outputY", 300); 14 intent.putExtra("return-data", true); 15 intent.putExtra("noFaceDetection", true); 16 System.out.println("22================"); 17 startActivityForResult(intent, PHOTO_REQUEST_CUT); 18 }
自定义对话框:
1 mDialog = new AlertDialog.Builder(this, R.style.FullScreenDialog) 2 .create(); 3 if (mDialog != null && !mDialog.isShowing()) { 4 mDialog.show(); 5 mDialog.setContentView(R.layout.dialog_select_imge); 6 mDialog.setCanceledOnTouchOutside(false); 7 }
自定义style :
1 <style name="FullScreenDialog" parent="android:style/Theme.Dialog"> 2 <item name="android:windowNoTitle">true</item> 3 <item name="android:windowFrame">@null</item> 4 <item name="android:windowIsFloating">true</item> 5 <item name="android:windowIsTranslucent">false</item> 6 <item name="android:background">@android:color/transparent</item> 7 <item name="android:windowBackground">@android:color/transparent</item> 8 <item name="android:backgroundDimEnabled">true</item> 9 </style>